4

I have a lot of has_many :through relations in my app. I am extensivley showing informations related to this, such as number of connected objects. Whenever user updates the relation, join table is modified, and I can catch this my Sweepers.

The problem is, that join table entries are deleted, not destroyed. If relation is gone, I have no resonable way to detect this, and I am displaying misleading informations from the cache. Everything like :touch => true, or :counter_cache => true works partialy. It's get incremented if relations are updated or created. But if user removes relation nothing happens. :counter_cache is getting broken, :touch doesn't trigger.

The garbage solution is to call .touch in the controller, when the main model is saved. This kind of works, but it seems really non-professional. This should be in the model logic, not in the controllers.

I feel like I am missing something big, but cant get my head over this. Anyone could put some insight on this problem?

mdrozdziel
  • 5,528
  • 6
  • 39
  • 55

2 Answers2

1

Monkey patching Active Record isn't necessary. When defining your association, set the :dependent option to :destroy.

class Book < ActiveRecord::Base
  has_many :authorships, :dependent => :destroy
  has_many :authors, :through => :authorships, :dependent => :destroy
end
laserlemon
  • 11
  • 1
  • 1
  • If you want callbacks for join model only (`authorships`), you just need to add `dependent: :destroy` on the associated model (`authors`) – tight Jun 21 '13 at 12:03
0

Check the monkey-patch that Mark S. wrote to answer his own question: How to create a full Audit log in Rails for every table?

ActiveRecord::Associations::HasManyThroughAssociation.class_eval do 
  def delete_records(records)
    klass = @reflection.through_reflection.klass
    records.each do |associate|
      klass.destroy_all(construct_join_attributes(associate))
    end
  end
end

It may be useful for your problem as well. Note that this was in Rails 2.. things may be different if you're already using Rails 3.

Community
  • 1
  • 1
Chris
  • 632
  • 4
  • 11
  • 18