-
-
Save jeffnash/b2bd040a979806e51de16b8392d8eef3 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
| field = Movie.new(:title => 'Title', | |
| :release_date => '21-Apr-89', :rating => 'PG') #just makes a new object, doesn't add it to DB | |
| field.save! #now it's saved in DB, the '!' means an exception will be thrown if it can't be saved | |
| field.title = 'New' #changes the object field only, it is still 'Title' in the DB unless we save again | |
| #### 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 | |
| #### Find returns an enumerable | |
| Movie.where('rating = "PG"').each do |mov| | |
| mov.destroy #after calling destroy, it's gone from DB. | |
| #You can still inspect the object but trying to modify it | |
| #or call a method that would access db would raise exception | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment