Advertisement
saasbook

association1.rb

Feb 20th, 2012
1,703
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.51 KB | None | 0 0
  1. # it would be nice if we could do this:
  2. inception = Movie.find_by_title('Inception')
  3. alice,bob = Moviegoer.find(alice_id, bob_id)
  4. # alice likes Inception, bob hates it
  5. alice_review = Review.new(:potatoes => 5)
  6. bob_review   = Review.new(:potatoes => 2)
  7. # a movie has many reviews:
  8. inception.reviews = [alice_review, bob_review]
  9. inception.save!
  10. # a moviegoer has many reviews:
  11. alice.reviews << alice_review
  12. alice.save!
  13. # can we find out who wrote each review?
  14. inception.reviews.map { |r| r.moviegoer.name } # => ['alice','bob']
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement