Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7667561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:08:11+00:00 2026-05-31T15:08:11+00:00

This is similar to a previous question, but our approach has changed a little

  • 0

This is similar to a previous question, but our approach has changed a little since.

I need to be able to destroy the associations between Releases & Tracks + Products & Tracks without destroying the track itself. I need to then be able to re-associate a Track with any Release or Product.

My models are as follows:

 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
  has_and_belongs_to_many :products
end

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

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

Can anyone help with the appropriate controller methods please?

If I use the following on a Product Track for example, it destroys the track and association:

 @product = Product.find(params[:product_id])
 @track = @product.tracks.find(params[:id])
 @track.destroy

I’ve tried something like this but it gives me a notmethoderror:

 @product = Product.find(params[:product_id])
 @track = @product.products_tracks.find(params[:track_id])
 @track.destroy

Can anyone point me in the right direction? As I say, I need this primarily on destroy, but i also need to then be able to re-create associations.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T15:08:12+00:00Added an answer on May 31, 2026 at 3:08 pm

    has_and_belongs_to_many (HABTM) is one way of declaring a many-to-many relationship between two models. Under the covers, rails uses a simple join table (a table with two columns only, each a foreign key to the primary key of the main tables). The association exists when a record in the join table exists. I am not sure how destroy works by default in this case, but it may decide to delete all of the associated records (not just the association). You may be able to control this by using the dependent <action> clause on the HABTM declaration. The relationship is created when you assign one or more “children” to a parent and save.

    In this case, maybe what you want is a pair of has_many :through relations — the “through” is a relation, but can be treated as a first class model, and extended and operated on with ActiveRecord, meaning you can get at a specific release track (for example) and decide to delete it without affecting either association.

    There’s a good section on when to use one vs. the other in this Rails Guide: http://guides.rubyonrails.org/association_basics.html#choosing-between-has_many-through-and-has_and_belongs_to_many

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is similar to my previous question but it didnt work with the Kindle
I know this question is similar to several previous ones, but I can't find
This question is similar to my previous question but not the same ... please
I thought to ask this as an update to my previous similar question but
This is similar to my previous question, but that solution did not solve this
This is similar to a previous question , but the answers there don't satisfy
This question is similar to a previous question of mine , but I formulated
This is very similar to a previous question (and may be the exact same
This post is very similar to my previous one, but the data structures are
I have checked this similar question, but the suggestions did not solve my problem:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.