Ruby on rails 参数处于状态的活动记录联接
我不确定我是否知道在活动记录中编写此sql查询的最佳方法,而不是完全手工编写。本质上,我想编写这两个查询的组合 这个查询本质上就是我试图重新创建的连接。我想确保返回的所有条目都是图像路径处理标志设置为false的图像Ruby on rails 参数处于状态的活动记录联接,ruby-on-rails,activerecord,Ruby On Rails,Activerecord,我不确定我是否知道在活动记录中编写此sql查询的最佳方法,而不是完全手工编写。本质上,我想编写这两个查询的组合 这个查询本质上就是我试图重新创建的连接。我想确保返回的所有条目都是图像路径处理标志设置为false的图像 @entries = ContestEntry.joins(:entry_images).where(contest_id: @contest.id, entry_images: { image_path_processing: false }).l
@entries = ContestEntry.joins(:entry_images).where(contest_id: @contest.id, entry_images: {
image_path_processing: false
}).limit(10)
然而,我在编写查询时遇到的问题是,我需要从url中包含参数,并且我不确定上述语法是否正确地删除了sql_注入
下面是我试图添加条件连接的内容
@entries = ContestEntry.where("contest_id = ? and created_at > ?",
params[:contest_id], Time.at(params[:after].to_i + 1))
似乎是从用户输入的结果中避免sql注入的完美方法…您能试试吗
@entries = ContestEntry.joins(:entry_images).
where(contest_id: params[:contest_id],
entry_images: { image_path_processing: false }).
where('contest_entries.created_at > ?', Time.at(params[:after].to_i+1)).
limit(10)
首先,阅读本文档: 当时,主要支持的语法是
ContestEntry.where("contest_id = ?", params[:contest_id])
但是现在您也可以使用更简单、更具ruby风格的
ContestEntry.where(:contest_id => params[:contest_id])
这两种方法都将通过sql消毒器传递参数,以防止文档中描述的sql注入攻击
还有几点:
sql_injection_params = "anything' OR 'x'='x"
# This string variant tells active record to use sql as is
Model.where("name = '#{sql_injection_params}'").to_sql # bad
# This array variant tells active record to sanitize
Model.where('name = ?', sql_injection_params).to_sql # good
# This hash variant tells active record to sanitize
Model.where(:name => sql_injection_params).to_sql # good
调用
to_sql
将显示根据查询生成的原始sql活动记录。只需更改上面的示例以使用您的数据,就可以更好地了解rails在引擎盖下的工作 感谢您对sql注入的解释。当前的讨论有点混乱:)。这很接近,我不得不添加竞赛项目。创建_at>以便在正确的表格上执行检查。竞赛项目。创建项目。atI知道这一点!啊哈,我很高兴我的解决方案帮助了你!谢谢你,Yoshi你救了我一天!
sql_injection_params = "anything' OR 'x'='x"
# This string variant tells active record to use sql as is
Model.where("name = '#{sql_injection_params}'").to_sql # bad
# This array variant tells active record to sanitize
Model.where('name = ?', sql_injection_params).to_sql # good
# This hash variant tells active record to sanitize
Model.where(:name => sql_injection_params).to_sql # good