2

I have 2 records of the same model, and I want to keep some of the data on these records in sync.

I was going to do a after_save callback (or maybe observer) to trigger updating the other record, but I am afraid this is going to cause an infinite loop of saves because the other record will cause a callback.

I read here that you can bypass callbacks on save, but these approaches seem to be hackish and not consistent between rails 2 and 3 (we are moving to rails 3 in a couple months).

Is there a better option?

Community
  • 1
  • 1
Joelio
  • 4,621
  • 6
  • 44
  • 80

2 Answers2

0

You can use the update_columns method while updating the 2nd record based on updates on the first one and vice versa.

Sachin Singh
  • 993
  • 8
  • 16
0

You can create attr_accessor:

attr_accessor :dont_run_callback

after_save :my_callback

def my_callback
  MyModel.find(1).update_attributes(..., :dont_run_callback => true)  unless dont_run_callback
end

something like that

antonversal
  • 1,219
  • 9
  • 10