Callbacks when deleting self-referenced has_many?

Hi

I’ve come across a behavior which doesn’t seem to be consistent. Let’s
assume the following models:

class Client << ActiveRecord::Base
has_many :resale_clients, :foreign_key => ‘reseller_id’
end

class ResaleClient << Client
belongs_to :client, :foreign_key => ‘reseller_id’
after_save :ack
def ack
puts “triggered”
end
end

Now in the console (assuming two Client records exist in the db):

c = Client.first
r = ResaleClient.find(2)
c.resale_clients << r # outputs “triggered”
c.resale_clients.delete® # does not output “triggered”

Any idea what I’m doing wrong here?

Many thanks!!

Figured it out, I should be using the association callbacks. For some
odd reason, I’ve never needed them up until now:

http://guides.rubyonrails.org/association_basics.html#association-callbacks

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs