Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- the gem active record that allows you to write ruby and translates from ruby to sql.
- Select *
- is Recipe.all
- when you do where is always a collection even if its just one
- &discount=true
- Recipe.count <<< alias for Recipe.length
- Recipe.sum(:prep_time) <<< sum up prep time
- Recipe.average(:prep_time)
- find_by always returns a single instance
- Recipe.all.order(:chef) <<< orders by chef
- Recipe.all.order(chef: :desc) < order desc
- Recipe
- Select *
- FROM recipes
- WHERE chef = "Rachel Ray";
- Recipe.where(chef: "Rachel Ray")
- Rails c << irb
- Recipe.all << to show all
- use Recipe.where("chef=?", "Rachel Ray")
- Recipe.where('title iLIKE ?', query) always use this method to prevent sql injection attacks.
- Do not use interpolation like Recipe.where('title iLIKe '#{query}'') never do this sql injections attacks very likely
- Recipe where if you want a collection
- Recipe.where(chef: "Josh")
- Recipe find_by() if you want to find one
- Recipe.find_by(chef: "Josh") <<< find by the first one or
- Recipe.find(2) <<< 2 is the id
- Query comands
- SELECT title
- FROM recipes;
- SELECT title, chef, created_at
- FROM recipes;
- SELECT *
- FROM recipes; <<< returns all info
- WHERE chef = 'Josh';
- only postgress uses single ''
- SELECT AVG(prep_time)
- FROM recipes;
- SELECT AVG(prep_time)
- FROM recipes;
- SELECT SUM (prep_time)
- FROM recipes;
- SELECT *
- FROM recipes
- WHERE chef iLIKE '%Josh%';
- iLIKE is case insensitive
- SELECT *
- FROM recipes
- WHERE prep_time >= 60
- ORDER BY prep_time DESC
- LIMIT 1;
- ORDER BY <<< does the order
- LIMIT 1;
- ORDER BY prep_time DESC(ASCD).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement