33

In Rails, when I want to find by a user given value and avoid SQL injection (escape apostrophes and the like) I can do something like this:

Post.all(:conditions => ['title = ?', params[:title]])

I know that an unsafe way of doing this (possible SQL injection) is this:

Post.all(:conditions => "title = #{params[:title]}")

My question is, does the following method prevent SQL injection or not?

Post.all(:conditions => {:title => params[:title]})

3 Answers 3

39

Yes, it does. Only the second one is dangerous.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your direct response.
8

One good reference from the RoR Guides.

Comments

5

+1 @fphilipe and @yuval Check this 5 min video from railscast and this one from rails guide

1 Comment

Thanks, I've seen this already but it doesn't cover my question (pertaining to the last find)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.