Advertisement
saasbook

association1.rb

Feb 13th, 2016
235
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. # a moviegoer has many reviews:
  10. alice.reviews << alice_review
  11. bob.reviews << bob_review
  12. # can we find out who wrote each review?
  13. inception.reviews.map { |r| r.moviegoer.name } # => ['alice','bob']
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement