简体   繁体   中英

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?

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

您不需要对文字使用参数化查询,只需执行以下操作:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM