【问题标题】:Activerecord: query with true does not show any result for boolean fieldsActiverecord:查询为 true 时不显示布尔字段的任何结果
【发布时间】:2016-12-20 00:48:19
【问题描述】:

我有一个users 表,其中一个布尔字段处于活动状态。我在数据库中有active: true 的用户。问题,下面的查询没有给出任何结果

@users = User.where(active: true)  #this results to empty array

但如果我给出 1 而不是 true,那么我会得到结果。

@users = User.where(active: 1)  #this gives correct result

我不明白为什么 true 不起作用。

【问题讨论】:

  • 架构文件中的用户活动设置为布尔值吗?
  • 这会为您的 sql 客户端 SELECT "users"."id", "users"."active" FROM "users"; 上的 users 表上的 active 列返回什么? true/false0/1 ?
  • 它返回 0/1,但我的架构说字段 active 是布尔值,而且当我运行像 User.first.active 这样的查询时,这个结果是 true 而不是 0/1
  • 你用的是什么数据库?
  • 我正在使用 sqlite

标签: ruby-on-rails ruby-on-rails-4 activerecord


【解决方案1】:

SQLite 没有单独的布尔存储类。反而, 布尔值存储为整数 0(假)和 1(真)。

您可以查看data type of SQLite here

因此,您使用活动 true 保存用户,但在数据库中 active 字段具有值 1,这就是 User.where(active: 1) 起作用的原因。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-12
    • 2022-01-08
    相关资源
    最近更新 更多