Advertisement
saasbook

association1.rb

Jan 10th, 2012
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.65 KB | None | 0 0
  1. # it would be nice if we could do this:
  2. inception = Movie.find_by_title('Inception')
  3. alice = Moviegoer.find_by_username('alice')
  4. bob =   Moviegoer.find_by_username('bob')
  5. # add reviews of Inception from figure
  6. alice_review_inception = Review.new(:potatoes => 5)
  7. bob_review_inception   = Review.new(:potatoes => 3)
  8. # the one-to-many association from movies to reviews:
  9. inception.reviews = [alice_review_inception, bob_review_inception]
  10. inception.save!
  11. # and from moviegoers to reviews:
  12. alice.reviews = [alice_review_inception]
  13. alice.save!
  14. bob.reviews  =  [bob_review_inception]
  15. bob.save!
  16. # can we find out who wrote a review?
  17. bob_review_inception.moviegoer  # => bob
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement