I have a model that I'll call Parent. When this model is updated, I register which User did this last update on the parent model. I do this at the ParentsController like this:
def update
@parent = Parent.find(params[:id])
@parent.assign_attributes(params[:parent])
@parent.last_editor = current_user
if @parent.save
# continue....
This works well. The controller tells the model who is the current_user. The concerns are separated well.
But the Parent model has many different child models and I want to update the last_editor of the Parent when its childs are updated. (The last_editor is a User).
I could do this:
class ChildOnesController
# ...
def create
@child_one = ChildOne.new(params[:child_one])
if @child_one.save
@child_one.parent.update_attribute(last_editor: current_user)
else
# ...
end
end
def update
@child_one = ChildOne.find(params[:id])
if @child_one.update_attributes(params[:child_one])
@child_one.parent.update_attribute(last_editor: current_user)
# continue....
end
end
def destroy
# Destroy...then
@child_one.parent.update_attribute(last_editor: current_user)
# ...
end
The main problem is: As my Parent model has various child models, I'll have to add those lines to every child model controller.
Is there a better, more concise way to do this?
Thanks a lot for any hint!!!