开发者

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

开发者 https://www.devze.com 2023-02-11 23:07 出处:网络
I\'m running the following query @projects = @company.projects.where(\"active = ?\", true).order(\"created_at ASC\")

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")
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号