Skip to content

Instantly share code, notes, and snippets.

@Papillard
Created August 1, 2016 10:19
Show Gist options
  • Save Papillard/fff8ed7e9b871097bfd4e5f5d89d912e to your computer and use it in GitHub Desktop.
Save Papillard/fff8ed7e9b871097bfd4e5f5d89d912e to your computer and use it in GitHub Desktop.
Active Record most useful methods
# 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