0

I have two models: team and season associated so that a team can belong to many seasons and each season can also have many teams. So far I have used an simple HABTM relationship between the models using a join table seasons_teams without an ID attribute.

Now I would like to add a hook for when an association is deleted, to be executed when a team drops out from a season. Is it correct that the best way to do this is to transform the HABTM association into a has_many / :trough, adding an ID attribute to what was the join table and creating the corresponding model file that will contain the new before_destroy hook? If so, how do I write the migration to add an auto-incremented index to my join table? (Or would it be better to create a new join table/model with index and to copy all the entries in the existing table)

2 Answers2

3

Following the Rails Style Guide:

Prefer has_many :through to has_and_belongs_to_many. Using has_many :through allows additional attributes and validations on the join model

In your case:

class SeasonTeam < ActiveRecord::Base # couldn't find a better name...
  belongs_to :team
  belongs_to :season
  # the validates are not mandatory but with it you make sure this model is always a link between a Season and a Team
  validates :team_id, :presence => true
  validates :season_id, :presence => true

  before_destroy :do_some_magic

  #...      
end

class Season < ActiveRecord::Base
  has_many :teams, :through => :season_teams
end

class Team < ActiveRecord::Base
  has_many seasons, :through => :season_teams
end
MrYoshiji
  • 54,334
  • 13
  • 124
  • 117
  • Yes, thanks. This is what I thought. It works. I created a completely new table to replace the join table and copied all the existing data into it also in the migration. – Christer Fernstrom Nov 26 '12 at 22:57
0

You could also look at Rails' Association Callbacks. It provides before_remove and after_remove callback methods which you can use to customize behaviour.

pungoyal
  • 1,768
  • 15
  • 17