In Rails I use MySQL and have
SnCl.all(:conditions => "col3=\"xx\"")
but it does not work with my Postgres DB in Heroku
As mentioned in the comments, most databases uses single quotes for string literals and double quotes for identifiers. MySQL is rather lax and will also accept double quotes for string literals but PostgreSQL is (thankfully) quite strict. So you want to use single quotes:
SnCl.all(:conditions => "col3 = 'xx'")
or using where:
SnCl.where("col3 = 'xx'")
or with sensible use of the database driver's quoting facilities:
SnCl.where("col3 = #{SnCol.connection.quote('xx')}")
And saving the best for last, the way sensible people do it using a placeholder or Hash arguments to where:
SnCl.where('col3 = ?', 'xx')
SnCl.where('col3 = :col3', :col3 => 'xx')
SnCl.where(:col3 => 'xx')
The final one would be the most idiomatic for Rails and the two above it would be useful for more complex conditions where chaining is either too cumbersome or doesn't work (such as when you need an OR in your WHERE clause).
You'd be better off using where for your condition. Have a look at
http://guides.rubyonrails.org/active_record_querying.html#conditions
来源:https://stackoverflow.com/questions/9230640/how-do-you-quote-strings-in-postgres