This project is archived and is in readonly mode.
AssociationCollection + Optimistic Locking + Save callback break things
-
mail2lf (at gmail)
Crap, guys, I'm new here, sorry for breaking the code text a bit:
class User has_many :posts endclass Comment belongs_to :user
after_save :update_user
def update_user
// do some weird stuff user.saveend end
-
mail2lf (at gmail)
- Assigned user set to DHH
-
schorsch
I have similar probs with callbacks from related records changing their parents. In my case its an invoice which is to be destroyed. Its attaxched payments have a callback which changes the status of the parent invoice in an after_destroy callback.
when the transaction reaches the invoice its locking version has been increased and the destroy fails.
@@@ruby class Payment
belongs_to :invoice
after_destroy :change_status
def change status #change the invoice unless self.invoice.to_be_destroyed
end
end
I had to enhance the stale error output in /activerecord/lib/active_record/locking/optimistic.rb to track my probs down Yes it might be a bad design and i'll try to avoid such things in the future but for now i need a way to figure out if the object referenced in the callback in inside a (destroy) transaction. I tried setting an instance var @to_be_destroyed but as i just read both objects(invoice / payment.invoice) are no equal. Maybe a thread variable could help or an explicit destroy switch in the db-table? Or has anybody another suggestion? -
schorsch
I have similar probs with callbacks from related records changing their parents. In my case its an invoice which is to be destroyed. Its attaxched payments have a callback which changes the status of the parent invoice in an after_destroy callback.
when the transaction reaches the invoice its locking version has been increased and the destroy fails.
class Payment
belongs_to :invoice
after_destroy :change_status
def change status #change the invoice unless self.invoice.to_be_destroyed
end
end
I had to enhance the stale error output in /activerecord/lib/active_record/locking/optimistic.rb to track my probs down
Yes it might be a bad design and i'll try to avoid such things in the future but for now i need a way to figure out if the object referenced in the callback in inside a (destroy) transaction.
I tried setting an instance var @to_be_destroyed but as i just read both objects(invoice / payment.invoice) are no equal. Maybe a thread variable could help or an explicit destroy switch in the db-table?
Or has anybody another suggestion?
.... to dumb for using formatting
-
Neeraj Singh
- Milestone changed from 3.x to 2.x
- Importance changed from to
I am not able to reproduce this in rails 3.
ActiveRecord::Schema.define(:version => 20100628071959) do create_table "brakes", :force => true do |t| t.string "name" t.integer "car_id" end create_table "cars", :force => true do |t| t.string "name" t.integer "lock_version", :default => 0 end end class Brake < ActiveRecord::Base belongs_to :car after_save :update_car def update_car car.save end end class Car < ActiveRecord::Base has_many :brakes end Car.create Car.first.brakes.create Car.first.brakes.create Car.create Car.last.brakes = [Brake.first, Brake.last] -
Santiago Pastorino
Neeraj can you try this on 2-3-stable?
-
Neeraj Singh
works fine on latest 2-3-stable.
-
Santiago Pastorino
mail2lf, schorsch: can you try the git versions of rails 3 and 2-3 to see if you can reproduce the problem?.
Thanks. -
Neeraj Singh
- State changed from new to invalid
Closing this ticket. If it could be reproduced then it will be opened.
-
Jeff Kreeftmeijer
Automatic cleanup of spam.