Hi,
I need some help understanding when fields of a Rails model are
updated. In this case, it’s about the fields of a has_many collection.
I have a hypothetical model beehive, which has_many(:bees). In the
controller, I do (example is abstracted):
bees = Array.new
params[“bees”].each_value do |bee_attributes|
bee = beehive.bees.build(bee_attributes)
bees.push(bee)
end
beehive.bees = bees
This all works fine, except when I try to access the bees of beehive
again, which I need for validations. I made a validate method in
beehive which runs by all the bees to check some stuff (what is not
important). The problem is, that the bees collection I have access to
in the validate method still contains the values from the DB, which
were loaded when this model was loaded with an “edit_beehive” action.
Similarly, if I do this:
beehive.bees = bees # the line from above
bees = beehive.bees
my bees have all been reverted to before I edited them.
I really don’t understand this. Some help would be appreciated.