导轨4.x的如何查询布尔值的ActiveRecord? [英] Rails 4.x how to query boolean value with activerecord?
问题描述
我的布尔猜对我的模式特殊待遇,我试图让那些拥有猜到津贴所有的客户都是真的。
I a boolean "guessed" on my model "perks" and I am trying to get all customers that have perks where guessed is true.
我的第一个猜测是这样的:
My first guess was this:
@customer = Customer.includes(:perks).where('perks.guessed = true')
但是,这给了我一个SQL异常:没有这样的列:真正的
But that gives me an SQL-exception: "no such column: true"
使用Google的一点给了我这个线程:<一href="http://stackoverflow.com/questions/5015340/rails-how-to-search-based-on-a-boolean-field-mysql-error">Rails - 如何搜索基于布尔型字段? (MySQL错误)
A bit of googling gave me this thread: Rails - how to search based on a boolean field? (MySQL error)
所以,我想:
@customer = Customer.includes(:perks).where('perks.guessed = 1')
没有运气...
也试过:
@customer = Customer.includes(:perks).where('perks.guessed LIKE, ?', 'true')
@customer = Customer.includes(:perks).where('perks.guessed = t')
什么是查询一个布尔带,其中语句中的ActiveRecord的价值的正确方法?
What is the correct way to query for the value of a boolean with a where-statement in activerecord?
我使用SQLite的开发和Postgres的生产顺便说一句。
I'm using SQLite on dev and Postgres on production btw.
推荐答案
这应该工作
@customer = Customer.includes(:perks).where('perks.guessed = ?', true)
或
看你的查询之一
本 @customer = Customer.includes(:福利)。凡('perks.guessed = T')
应
@customer = Customer.includes(:perks).where("perks.guessed = 't'")
这篇关于导轨4.x的如何查询布尔值的ActiveRecord?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!