Created
April 29, 2016 18:01
-
-
Save armandofox/815530cfaf9b20848c3672360d0436f2 to your computer and use it in GitHub Desktop.
activerecord1.rb
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
| #### Create | |
| starwars = Movie.create!(:title => 'Star Wars', | |
| :release_date => '25/4/1977', :rating => 'PG') | |
| # note that numerical dates follow European format: dd/mm/yyyy | |
| requiem = Movie.create!(:title => 'Requiem for a Dream', | |
| :release_date => 'Oct 27, 2000', :rating => 'R') | |
| # Creation using separate 'save' method, used when updating existing records | |
| field = Movie.new(:title => 'Field of Dreams', | |
| :release_date => '21-Apr-89', :rating => 'PG') | |
| field.save! | |
| field.title = 'New Field of Dreams' | |
| #### Read | |
| pg_movies = Movie.where("rating = 'PG'") | |
| ancient_movies = Movie.where('release_date < :cutoff and rating = :rating', | |
| :cutoff => 'Jan 1, 2000', :rating => 'PG') | |
| #### Another way to read | |
| Movie.find(3) # exception if key not found | |
| #### Update | |
| starwars.update_attributes(:description => 'The best space western EVER', | |
| :release_date => '25/5/1977') | |
| requiem.rating = 'NC-17' | |
| requiem.save! | |
| #### Delete | |
| requiem.destroy | |
| Movie.where('title = "Requiem for a Dream"') | |
| #### Find returns an enumerable | |
| Movie.where('rating = "PG"').each do |mov| | |
| mov.destroy | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment