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::Statem开发者_如何学JAVAentInvalid: 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?
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 :) )
You don't need to use parameterized queries with literals, just do this:
@projects = @company.projects.where("active = 1").order("created_at ASC")
精彩评论