将属性更新为零
答
@my_obj = MyObject.find(params[:id])
if params[:type] == @my_obj.type
@my_obj.update_attribute(:type, nil)
end
答
其实做到这一点的最好办法是一样的东西
params[:your_object][:test] = nil if params[:your_object][:test] == @your_object.type
@your_object.update_attributes(params[:your_object])
(简码:看params[:your_object]
重复 - >应该重构)
你也可以想干什么它分两步:首先提取类型,然后更新属性,但我认为这是更多的工作。
received_type = params[:your_object].delete(:type)
received_type = nil if received_type == @your_object.type
@your_object.update_attribute :type, received_type
#still do the rest of the update, without the type
@your_object.update_attributes(params[:your_object])
希望这会有所帮助。
我们不能像测试如果PARAMS [:类型] == @ my_obj.type @ my_obj.update_attribute(:型,无) 结束 – user315252 2011-02-08 09:05:58