I have a two models, A
and B
where A
has_many
B
.
In the A
model, I have a public method that loops through all of the associated B
objects and updates database fields in each of them. I would like to have the methods in B
and run just the loops in A
, but they do not appear to be saving correctly. Is there something wrong with this approach?
Here's an example of the methods:
def update_fields # A Method
self.b.each do |b|
b.b_method
end
end
def b_method # B Method
self.field = '5'
self.save
end
Running this type of code, nothing is saved to the database and the output isn't updated.
How can I accomplish this type of thing? Or, does all the logic need to be in the A
controller, even though it's dealing with B
fields?
It ended up being a problem with the validations, so the general approach worked.