I have been working with rails for almost a year now and I have been in situations where I am not sure when to use the many-to-many association. This is my current situation.
I have a player, season, and sport.
player.rb
has_many :seasons, :dependent => :destroy
season.rb
belongs_to :player
has_many :sports, :dependent => :destroy
sport.rb
belongs_to :season
A player has_many :seasons and season belongs to player. Then I have sport which belongs to season and seasons has_many :sports.
What I would like is for a player to add a new season(ex. 2011-2012, 2012-2013, 2014-2015 etc) then after the season is created I would like for the player to add the sports they are currently playing that season. I am very confuse at this point because the way I am doing it now forces me to use nested resources that are 3 level deep like so
routes.rb
resources players do
resources :seasons do
resources :sports
end
end
If I were to use 3 level deep nested resources I would have to add players to my form that only accepts 2 options.
_form.html.erb
<%= form_for([@season, @sport]) do |f| %>
Would i use a many-to-many association for this or how should I approach this?
Maybe this will help you clear up some many-to-many questions you have:
Many-to-Many
With your routes, you may not have to nest your routes so deeply… separating may be a better approach.(up to you on how you want your routing)
I see a few many-to-many relationships forming. A player can play many sports, and a perticular sport could have many players. A season can have many different sports, and a sport can have multpile seasons through the years.
Once you have your models figured out, if you want a form that manipulates two different models, you should look into nested_forms
Nested_forms
Hopefully this will help you get started/clear some things up for you!
I too built a sports app(basketball), and ran into some issues with many-to-many relationships and nesting forms here. Maybe you can use this as reference/guide along the way