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 502315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:16:43+00:00 2026-05-13T06:16:43+00:00

Through many iterations of testing, I just noticed that my join table that represents

  • 0

Through many iterations of testing, I just noticed that my join table that represents a HABTM relationship between two models isn’t removing entries when instances of these models get deleted. Do I need to do something special when removing an instance of a model that has HABTM relationships?

  • 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-13T06:16:44+00:00Added an answer on May 13, 2026 at 6:16 am

    Upon closer inspection HABTM relationships should be removing join table entries. However neither HABTM relationships or the relationship I described in the original version (see post history) of this solution will remove those join table entries when you eliminate the record with the delete method. ActiveRecord::Base#delete does not trigger any callbacks, such as the ones a HABTM relationship establishes to remove orphaned entries from the join table. Instead you should be using ActiveRecord::Base#destroy.

    You will have to use raw SQL to remove the unneeded entries. If you decide to create a join model, you can iterate through entries in the join model, deleting those without an association.

    Example:

    class Foo < ActiveRecord::Base
      has_many :foo_bars, :dependent => :destroy
      has_many :bars, :through => :foo_bars
    end
    
    class FooBar < ActiveRecord::Base
      belongs_to :foo
      belongs_to :bar
    end
    
    class Bar < ActiveRecord::Base
      has_many :foo_bars, :dependent => :destroy
      has_many :foos, :through => :foo_bars
    end
    
    FooBar.all.each{|fb| fb.destroy? if fb.foo.nil? || fb.bar.nil? }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JavaScript function that takes two required parameters and then arbitrarily many
I am crunching through many gigabytes of text data and I was wondering if
While going through many resources on multithreaded programming, reference to volatile specifier usually comes
Following code iterates through many data-rows, calcs some score per row and then sorts
I've looked through many of the existing threads about this error, but still no
I've read through many of the drag and drop threads on SO and i
I have searched through many times but have not seen this before. Probably really
I've read through many of the questions on SO about this, but many answers
I've been running through many of the text to speech examples available for Android
basically my app has a loginScreen and once logged u can go through many

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.