Created
February 22, 2021 00:59
-
-
Save armandofox/620547b36e435fb06febcb6f889d829f to your computer and use it in GitHub Desktop.
association1.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
# it would be nice if we could do this: | |
inception = Movie.where(:title => 'Inception') | |
alice,bob = Moviegoer.find(alice_id, bob_id) | |
# alice likes Inception, bob less so | |
alice_review = Review.new(:potatoes => 5) | |
bob_review = Review.new(:potatoes => 3) | |
# a movie has many reviews: | |
inception.reviews = [alice_review, bob_review] | |
# a moviegoer has many reviews: | |
alice.reviews << alice_review | |
bob.reviews << bob_review | |
# can we find out who wrote each review? | |
inception.reviews.map { |r| r.moviegoer.name } # => ['alice','bob'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment