ruby on rails确实update_attributes可以防止sql注入吗?

前端之家收集整理的这篇文章主要介绍了ruby on rails确实update_attributes可以防止sql注入吗?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
update_attributes是否可以防止sql注入?

例:

if @user.update_attributes(params[:user])
     # updated
 end

我知道find(),{}和[]做find:conditions,但是没有看到关于这个方法的任何信息.

解决方法

@H_404_20@ 是的,它确实.在内部,它只是循环遍历所有属性,设置它们的值然后调用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

猜你在找的Ruby相关文章