Raoot
Raoot

Reputation: 1771

DELETE methods for HABTM associations?

I have the following models:

class Release < ActiveRecord::Base
  has_many :products, :dependent => :destroy
  has_and_belongs_to_many :tracks
end

class Product < ActiveRecord::Base
 belongs_to :release
 has_many :releases_tracks, :through => :release, :source => :tracks      
 has_and_belongs_to_many :tracks
 before_save do
   self.track_ids = self.releases_track_ids
 end        
end

class Track < ActiveRecord::Base
  has_and_belongs_to_many :releases
end

class ReleaseTracks < ActiveRecord::Base
  belongs_to :release
  belongs_to :track
end

class ProductsTracks < ActiveRecord::Base
  belongs_to :product
  belongs_to :track
end

At the moment I can create a release and add tracks to it. When I then create a product it inherits the tracks from the release.

What I want to do is be able to delete individual tracks at the product level, but not the track entry itself, so delete the association in ProductsTracks.

How would I go about writing the appropriate destroy method, which controller should it reside in and how should the link_to be structured?

Upvotes: 0

Views: 591

Answers (1)

jefflunt
jefflunt

Reputation: 33954

Have you tried just destroying the tracks at the product level? I believe the default behavior is to destroy the relationship, and not the record at the other end of the relationship.

Upvotes: 1

Related Questions