Skip to content

Instantly share code, notes, and snippets.

@samiron
Created October 10, 2012 16:52
Show Gist options
  • Save samiron/3866858 to your computer and use it in GitHub Desktop.
Save samiron/3866858 to your computer and use it in GitHub Desktop.
Rails 3: has_many :through
#FILE: models/user.rb
@samiron
Copy link
Author

samiron commented Oct 10, 2012

This is a complete demonstration of many-to-many relationship using has_many :through between two models.
Here we have Team and Player models where each can contain many of other.

Like, a team has many players and a player has many teams. They are related through Contract model which has player_id, team_id and offer columns.

The focus is to show

  • Nested forms to create two new resources while being connected.
  • Creating one new resource linked with an existing another resource.
  • Creating a relation between two existing records.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment