2016-11-23 64 views
1

我正在使用Rails项目并使用Brakeman作为调试工具。我用查询从表中获取数据,但在Brakeman的测试中,它指出查询中存在Sql Injection Possibility。无法防止SQL注入在Rails中的查询

这里是我的查询:

Applicant.all.where("profile_id=#{current_user.profile.id}").first 

但我不知道有什么问题与此查询,如果没有固定它,然后我怎么能阻止它的SQL注入?为此,根据轨道

+0

您已标记MySQL和SQL Server,它是哪一个? –

+0

试试这个Applicant.where('profile_id =?',current_user.profile.id).first或Applicant.where(profile_id:current_user.profile.id).first –

+0

选中此项:http://guides.rubyonrails.org/ active_record_querying.html#pure-string-conditions –

回答

4

利用向导做正确的方式这

Applicant.where('profile_id = ?', current_user.profile.id).first 

OR 

Applicant.where(profile_id: current_user.profile.id).first 

OR 

Applicant.find_by_profile_id(current_user.profile.id) 

OR 

Applicant.find_by(profile_id: current_user.profile.id) 
0

我认为你在寻找的东西是:

Applicant.find_by(profile_id: current_user.profile.id) 

当你阅读这段代码更容易理解你在做什么。