Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

after_destroy callback is not triggered in call to collection_singular_id

#2292

I have a salesperson model:


class Salesperson < User has_many :dealers_and_salespeople, :class_name => "DealerAndSalesperson"

def dealer=(dealer)

self.dealer_ids = dealer.nil? ? [] : [dealer.id]

end ...

end

The dealer_and_salesperson model is like this:


class DealerAndSalesperson < ActiveRecord::Base set_table_name "dealers_and_salespeople" belongs_to :salesperson belongs_to :dealer

after_create :create_event_22 after_destroy :create_event_24

def create_event_22

Event.create(:event_type_id => 22, :salesperson_id => salesperson_id, :dealer_id => dealer_id)

end

def create_event_24

Event.create(:event_type_id => 24, :salesperson_id => salesperson_id, :dealer_id => dealer_id)

end

end

When I call salesperson.dealer = an_existing_dealer, the after_create callback is triggered. But when I call salesperson.dealer = nil, the after_destroy callback is not triggered at all.

Please let me know if this is by design, or I'm not using rails in the right way.

Thanks, Yi Zhang

Reported by Yi · March 18th, 2009 @ 07:53 PM

State: resolved
Milestone: 2.x
Assigned to: Ryan Bigg Ryan Bigg
Importance: none

Activity

  1. danny.chiu
    danny.chiu
    • Assigned user set to DHH

    I ran into the same question,the after_destroy callback did not work at all.
    My solution: changed after_destroy to before_destroy, and modified it. it worked finally!

    June 8th, 2009 @ 12:59 AM

  2. David Trasbo
    David Trasbo
    • Assigned user changed from DHH to Ryan Bigg

    Yi,

    If this issue is still relevant I think you should try Danny's suggestion.

    Unless it turns out his suggestion doesn't fix the problem I think this ticket should be closed.

    April 14th, 2010 @ 08:14 PM

  3. Ryan Bigg
    Ryan Bigg
    • State changed from new to resolved

    April 15th, 2010 @ 11:03 AM