Topic: Single Quote Escaping

Hi everyone!

I have a ruby-on-rails application that stores a list of strings. Some strings include a single quote, e.g. the word "don't". Because I have many such words, I use the rails console for adding new items. Storing them in the database is fine -- here I just escape the single quote

w.word = 'don\'t'
w.save

But now when I try to find this word again, I get problems

w.find(:all, :conditions=>["word='don\'t'"])

This no longer works. I get a syntax error. I also tried with three \ but this also yields a syntax error. What can I do?

Thank you very much for your help !!

Last edited by patrick007 (2011-04-10 13:06:22)

Re: Single Quote Escaping

You could try something like...

w.find(:all, :conditions=>["word=\"don't\""])   # escape the double quotes instead of the single quote
w.find(:all, :conditions=>["word=?", "don't"])   # move the value out of the string so that you avoid having to escape quotes

Re: Single Quote Escaping

Thank you!

The first possibility does not work - it creates a different error, albeit not a syntax error.

The second possibility work well. Thanks a lot!!