Reputation: 11960
I have the following model:
class TicketStatus < ActiveRecord::Base
after_update :set_previous_staff
attr_accessible :status_id, :ticket_id , :staff_id, :advisor_id, :previous_advisor_id, :previous_staff_id
belongs_to :status
belongs_to :ticket
belongs_to :staff, class_name: 'Staff', foreign_key: 'staff_id'
belongs_to :previous_staff , class_name: 'Staff', foreign_key: 'previous_staff_id'
belongs_to :advisor, class_name: 'Advisor', foreign_key: 'advisor_id'
def set_previous_staff
self.previous_staff_id = self.staff_id_was if self.staff_id_changed?
end
end
set_previous_staff
method is not working with the callback however i tried changed?
in console and it should work , what is missing here ? i am using rails 3.2.14
what i expected: I have an assigned employee to a ticket , i need when that assigned employee changed, his id should be added to previous_staff_id
, so this will let me know who is the current employee and who was before him.
Upvotes: 0
Views: 1534
Reputation: 11499
You're changing the instance after you update it, but you never actually save the change to the database after that. For updating, the order goes:
Try changing after_update
to before_update
so that your method runs before the change is committed.
Upvotes: 1