This project is archived and is in readonly mode.
after_create callback is not called in an association
Reported by Rafael Mendonça França | October 13th, 2010 @ 03:05 PM
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
Comments and changes to this ticket
-
99miles October 15th, 2010 @ 11:08 PM
This is working fine for me -- the callback is getting called in both cases.
-
Rafael Mendonça França October 15th, 2010 @ 11:31 PM
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">
-
Ryan Bigg October 16th, 2010 @ 02:22 AM
- Tag changed from sheepskin boots, callbacks to callbacks
- Importance changed from to Low
Automatic cleanup of spam.
-
Santiago Pastorino February 2nd, 2011 @ 04:47 PM
- 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.
-
Santiago Pastorino February 2nd, 2011 @ 04:47 PM
- State changed from open to stale
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>