Advertisement
Guest User

Untitled

a guest
May 19th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. the gem active record that allows you to write ruby and translates from ruby to sql.
  2.  
  3. Select *
  4. is Recipe.all
  5. when you do where is always a collection even if its just one
  6.  
  7. &discount=true
  8.  
  9.  
  10. Recipe.count <<< alias for Recipe.length
  11.  
  12. Recipe.sum(:prep_time) <<< sum up prep time
  13. Recipe.average(:prep_time)
  14.  
  15.  
  16. find_by always returns a single instance
  17.  
  18. Recipe.all.order(:chef) <<< orders by chef
  19. Recipe.all.order(chef: :desc) < order desc
  20.  
  21. Recipe
  22.  
  23. Select *
  24. FROM recipes
  25. WHERE chef = "Rachel Ray";
  26.  
  27. Recipe.where(chef: "Rachel Ray")
  28.  
  29. Rails c << irb
  30. Recipe.all << to show all
  31.  
  32. use Recipe.where("chef=?", "Rachel Ray")
  33.  
  34.  
  35. Recipe.where('title iLIKE ?', query) always use this method to prevent sql injection attacks.
  36. Do not use interpolation like Recipe.where('title iLIKe '#{query}'') never do this sql injections attacks very likely
  37.  
  38.  
  39. Recipe where if you want a collection
  40. Recipe.where(chef: "Josh")
  41.  
  42. Recipe find_by() if you want to find one
  43. Recipe.find_by(chef: "Josh") <<< find by the first one or
  44. Recipe.find(2) <<< 2 is the id
  45.  
  46.  
  47. Query comands
  48. SELECT title
  49. FROM recipes;
  50. SELECT title, chef, created_at
  51. FROM recipes;
  52.  
  53.  
  54. SELECT *
  55. FROM recipes; <<< returns all info
  56.  
  57.  
  58. WHERE chef = 'Josh';
  59.  
  60.  
  61. only postgress uses single ''
  62.  
  63. SELECT AVG(prep_time)
  64. FROM recipes;
  65.  
  66. SELECT AVG(prep_time)
  67. FROM recipes;
  68.  
  69.  
  70. SELECT SUM (prep_time)
  71. FROM recipes;
  72.  
  73. SELECT *
  74. FROM recipes
  75. WHERE chef iLIKE '%Josh%';
  76.  
  77. iLIKE is case insensitive
  78.  
  79. SELECT *
  80. FROM recipes
  81. WHERE prep_time >= 60
  82. ORDER BY prep_time DESC
  83. LIMIT 1;
  84. ORDER BY <<< does the order
  85. LIMIT 1;
  86. ORDER BY prep_time DESC(ASCD).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement