Let's say I have two models:
class Model1 < ActiveRecord::Base
has_many :model2
def save
self.attr = <sth. complex involving the associated model2 instances>
super
end
end
class Model2 < ActiveRecord::Base
belongs_to :model1
end
The statement in the overwritten save
method will issue a complex query (using find
[or alternatively named scopes]) to calculate some aggregate value for some of the associated Model2 instances. The problem is that when a new Model1 instance along with some Model2 instances, this query will not return anything on the first save
after the objects have been created and will return old data (the previous generation) for all consecutive save
operations.
Is there a way to use find
on the non-persisted in-memory state?