Answer the question
In order to leave comments, you need to log in
How to setup has_one association for model editing?
There is a model1 model, it has_one: model2
There is a route for making changes to model1:
class Model1Controller
def update
@model1 = Model1.where(params[:id]).first
@model1.with_lock do
@model1.update_attributes({:model1attr, :model2_attributes=>{:model2attr}})
if @model1.errors.empty?
flash[:success] = "все хорошо"
else
flash[:error] = "все плохо"
end
redirect_to кудабытонибыло
end
end
end
Answer the question
In order to leave comments, you need to log in
Updating a connected model is standard Rails behavior. If your application behaves differently, you need to understand, look at the accompanying code (models, views, at least).
I also suggest that you try to reproduce your error on a minimally working application (2 models and a controller), and post the code on Github.
I’ll also note that connected models are already updated through transactions, so the method with_lock
is superfluous.
The question is too complex. Are you wondering if it's possible to update updated_at on related models when editing the parent? You can, through a hook in the model, for example:
after_save do
model.update_attribute(:updated_at, Time.now)
end
model.touch
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question