Skip to content

Instantly share code, notes, and snippets.

@armandofox
Created February 22, 2021 00:59
Show Gist options
  • Save armandofox/38c7992c280c175cc6e732ee8b44157d to your computer and use it in GitHub Desktop.
Save armandofox/38c7992c280c175cc6e732ee8b44157d to your computer and use it in GitHub Desktop.
has_many_through_example.rb
# 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