update_attributes是否可以防止sql注入? 例: if @user.update_attributes(params[:user]) # updated end 我知道find(),{}和[]做find:conditions,但是没有看到关于这个方法的任何信息. 是的,它确实.在内部,它只是
例:
if @user.update_attributes(params[:user]) # updated end
我知道find(),{}和[]做find:conditions,但是没有看到关于这个方法的任何信息.
是的,它确实.在内部,它只是循环遍历所有属性,设置它们的值然后调用save!def update_attributes(attributes) with_transaction_returning_status do self.attributes = attributes save end end def attributes=(new_attributes, guard_protected_attributes = true) ... attributes.each do |k, v| if k.include?("(") multi_parameter_attributes << [ k, v ] elsif respond_to?("#{k}=") send("#{k}=", v) else raise(UnknownAttributeError, "unknown attribute: #{k}") end end end
换句话说,它的作用是什么
m.update_attributes(:attr1 => "foo", :attr2 => "bar") m.attr1 = "foo" m.attr2 = "bar" m.save