2016-11-23 9 views
1

私はRailsプロジェクトに取り組んでおり、デバッグ用のツールとしてBrakemanを使用しています。私はテーブルからデータを取得するためにクエリを使用しましたが、BrakemanのテストではクエリにSql Injection Possibilityがあると書いています。ここで RailsでSQLインジェクションからのクエリを防ぐことができません

は私のクエリです:

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 OR 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) 

あなたはこのコードを読んだとき、それは理解することは簡単ですあなたがしていること。

関連する問題