Hi,
how can i detect changed attributes, when i'm using update_attributes in the update method of my controller? A solution in the model would be very nice. Tanks
greetings Leon
Hi,
how can i detect changed attributes, when i'm using update_attributes in the update method of my controller? A solution in the model would be very nice. Tanks
greetings Leon
You can do it with update_attributes as that updates and saves in one go.
You can do this instead
@thing.attributes=(params[:thing]) if @thing.changed? @thing.save else ....something else end
All the best
RobL
Hi,
I thought about your solution and now I think i've got a better solution, what do you think about that? I just overwrite the update_attributes method and after i assigned the attributes, i can check for changes.
def update_attributes(attributes) self.attributes = attributes if self.changed? #kickoff something.... end save end
What do you think about this solution?
The problem with both yours and Rob's solution is that you're doing your post-update checks in one place, when there's more than one place those updates can happen. Your modification of Rob's suggestion is better, because at least you're checking in the model, rather than in the controller.
But you might be better off doing your check regardless of where the updates have come from (either a single attribute being assigned, or a mass allocation), so put a "before_save" filter in your model:
before_save :check_changed def check_changed puts (changed? ? "changed" : "unchanged") end
Of course, this might *cause* problems for your specific requirement, if, for instance, there's certain fields you want to ignore changes for. But you can always expand the "check_changed" method to do more work; check the changed_attributes hash, etc.