我需要检查表的多个列以查看是否找到匹配项。如果我找到匹配项,我需要使用我的所有表单参数“更新属性”匹配记录......否则我需要使用我的所有表单参数添加一个新记录。
if @somethingtoupdate = Model.where("column1 = ? and column2 = ?", params[:something][:column1], params[:something][:column2])
if @somethingtoupdate = Model.update_attributes(params[:something])
redirect_to somewhere_path, :notice => "The existing record was updated"
else
render "myformlocation"
end
else
@added = Model.new(params[:something])
if @added.save
redirect_to somewhere_path, :notice => "The new record was created"
else
render "myformlocation"
end
end
更新
@somethingtoupdate = Model.where("this_id = ? and that_id = ?", params[:something][:this_id], params[:something][:that_id])
if ! @somethingtoupdate.empty?
if @somethingtoupdate.update_attributes(params[:something])
redirect_to some_path, :notice => "The existing record was updated"
else
render "myformlocation"
end
else
@added = Model.new(params[:something])
if @added.save
redirect_to some_path, :notice => "The new record was created"
else
render "myformlocation"
end
end
感谢@micahbf,这就是我现在的立场。
但是,当有匹配的记录时,我的“update_attributes”仍然出现错误。
似乎这应该有效....我错过了什么或做错了什么?