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_create callback is not called in an association

#5800

I have the following scenario:

class A < AR::Base
  has_many :items, class_name => 'B'
end

class B < AR::Base
  belongs_to :a

  after_create :update_x

  private

  def update_x
    self.x.att += self.another_att
  end
end

When I call B.create the after_create callback is called. But when I call A.items.create the callback is not called.

I'm using Rails 3.0

Reported by Rafael Mendonça França · October 13th, 2010 @ 03:05 PM

State: stale
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. 99miles
    99miles

    This is working fine for me -- the callback is getting called in both cases.

    October 15th, 2010 @ 11:08 PM

  2. Rafael Mendonça França
    Rafael Mendonça França

    I tried it again in a new Rails 3 application and I have the same behavior.

    class Order < ActiveRecord::Base
      has_many :items, :class_name => 'OrderItem'
    end
    
    class OrderItem < ActiveRecord::Base
      belongs_to :order
    
      after_create :update_value
    
      private
    
      def update_value
        self.order.value += self.price
      end
      
    end
    

    In Rails console:

    >> order
    => #<Order id: 1, value: nil, created_at: "2010-10-15 22:21:36", updated_at: "2010-10-15 22:21:36">
    >> order.value = 0
    >> order.save!
    => true
    >> order.items.create(:price => 10)
    => #<OrderItem id: 1, price: 10, order_id: 1, created_at: "2010-10-15 22:22:54", updated_at: "2010-10-15 22:22:54">
    >> order
    => #<Order id: 1, value: 0, created_at: "2010-10-15 22:21:36", updated_at: "2010-10-15 22:22:52">
    >> order.items.create(:price => 10)
    => #<OrderItem id: 2, price: 10, order_id: 1, created_at: "2010-10-15 22:23:23", updated_at: "2010-10-15 22:23:23">
    >> order
    => #<Order id: 1, value: 0, created_at: "2010-10-15 22:21:36", updated_at: "2010-10-15 22:22:52">
    >> OrderItem.create!(:order => order, :price => 20)
    => #<OrderItem id: 3, price: 20, order_id: 1, created_at: "2010-10-15 22:23:48", updated_at: "2010-10-15 22:23:48">
    >> order
    => #<Order id: 1, value: 20, created_at: "2010-10-15 22:21:36", updated_at: "2010-10-15 22:22:52">
    

    October 15th, 2010 @ 11:31 PM

  3. Ryan Bigg
    Ryan Bigg
    • Tag changed from sheepskin boots, callbacks to callbacks
    • Importance changed from to Low

    Automatic cleanup of spam.

    October 16th, 2010 @ 02:22 AM

  4. Ryan Bigg
    Ryan Bigg
    • Tag cleared.

    Automatic cleanup of spam.

    October 19th, 2010 @ 08:35 AM

  5. Santiago Pastorino
    Santiago Pastorino
    • State changed from new to open

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    February 2nd, 2011 @ 04:47 PM

  6. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 04:47 PM