Boolean conditions in Rails find statements

I was having trouble getting a condition set on a standard find query. I had a table with a boolean column and was expecting to do something like this

a = model.find(:all, :conditions => 'checked = true')

But that (and variations on it) didn’t work, a bit of searching turned up a suitable solution here: included below for future reference:

class='ruby'>a = model.find(:all, :conditions => ['checked = ?', true])

That generates the correct SQL for whatever database back end your using.

Written on 22 Dec 2008 and categorised in Blogroll, Rails, and Ruby, tagged as SQL, Find, and Boolean

Home, Post archive

site copyright Eric Freeman

Valid XHTML 1.0 Strict