update_attributes 是否可以防止 sql 注入(inject)?
例子:
if @user.update_attributes(params[:user])
# updated
end
我知道 find() 和 {} 和 [] 在 find :conditions 中执行,但没有看到有关此方法的任何信息。
最佳答案
是的,它确实。在内部,它只是简单地循环所有属性,设置它们的值然后调用保存!
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
关于sql - ruby on rails update_attributes 是否可以防止 sql 注入(inject)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3192524/