Created
August 1, 2016 10:19
-
-
Save Papillard/fff8ed7e9b871097bfd4e5f5d89d912e to your computer and use it in GitHub Desktop.
Active Record most useful methods
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# For exhaustive documentation => http://guides.rubyonrails.org/active_record_basics.html | |
# Create a restaurant | |
fox = Restaurant.new(name: "the fox") | |
fox.save | |
# Update a restaurant | |
fox.address = "Bristol" | |
fox.save | |
# Read | |
restaurants = Restaurant.all # array of Restaurant objects! | |
restaurant_with_id_two = Restaurant.find(2) | |
first_restaurant_in_bristol = Restaurant.find_by_address("Bristol") | |
# Delete | |
restaurant_with_id_two.destroy | |
# Advanced queries | |
Restaurant.count | |
Restaurant.where(address: "London") | |
Restaurant.where("name LIKE ?", "%goose%") | |
Restaurant.order(created_at: :desc) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment