【发布时间】:2018-01-29 15:49:36
【问题描述】:
我一直在尝试根据某个查询检索一定数量的用户。我遇到的问题是我的结果差异很大,具体取决于我使用的是false 还是"false"。以下是我的查询:
User.all
.where.not(id:1)
.where.not("lower(promo) = ?","promo")
.where(created_at:7.months.ago..6.months.ago, auto_renew: false)
这将返回符合查询的三个用户的列表。但是当我运行以下命令时:
User.all
.where.not(id:1)
.where.not("lower(promo) = ?","promo")
.where(created_at:7.months.ago..6.months.ago, auto_renew: "false")
这将返回一个不在原始列表中的用户。 auto_renew 值应该是一个布尔值。为什么返回的内容会有所不同,我该如何解决?
编辑
根据要求,这是我的 users 架构:
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "first_name"
t.string "last_name"
t.boolean "auto_renew", default: true
end
【问题讨论】:
-
请向我们展示
users表的架构 -
好的。查看编辑。
-
我认为字符串
"false"不是false。为什么您会对不同条件的查询返回不同的结果感到惊讶? -
因为所有 4 个用户都应该满足一个查询或另一个。它们在两者中混杂在一起。它们都具有完全相同的值。
-
查看您的日志或
.to_sql并找出生成的查询之间的区别。
标签: sql ruby-on-rails ruby activerecord