Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails - how to search based on a boolean field? (MySQL error)

I'm running the following query

@projects = @company.projects.where("active = ?", true).order("created_at ASC")

and I'm getting the error:

`ActiveRecord::StatementInvalid: Mysql::ParseError: You have an error in your SQL...`

the error points to the = '1'.

I've tried many variations on my query but I cannot figure out the problem. How can I solve this?

like image 253
sscirrus Avatar asked Feb 16 '11 10:02

sscirrus


1 Answers

Try:

@projects = @company.projects.where(:active =>  true)

(it also works with strings 'active').

You can also look at http://guides.rubyonrails.org/active_record_querying.html#hash-conditions for more details.

There is also a nice railscast about this which explains why you might have problems (I'm not allowed to post 2 links so you should search for it :) )

like image 89
Calin Avatar answered Oct 15 '22 20:10

Calin