Created
February 22, 2021 00:59
-
-
Save armandofox/38c7992c280c175cc6e732ee8b44157d to your computer and use it in GitHub Desktop.
has_many_through_example.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
# in moviegoer.rb: | |
class Moviegoer | |
has_many :reviews | |
has_many :movies, :through => :reviews | |
# ...other moviegoer model code | |
end | |
alice = Moviegoer.where(:name => 'Alice') | |
alice_movies = alice.movies | |
# MAY work, but a bad idea - see caption: | |
alice.movies << Movie.where(:title => 'Inception') # Don't do this! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment