sscirrus
sscirrus

Reputation: 56709

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?

Upvotes: 11

Views: 14807

Answers (2)

Calin
Calin

Reputation: 1491

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 :) )

Upvotes: 30

Jacob Relkin
Jacob Relkin

Reputation: 163228

You don't need to use parameterized queries with literals, just do this:

@projects = @company.projects.where("active = 1").order("created_at ASC")

Upvotes: 8

Related Questions