【问题标题】:Boolean field being ignored in mongoid query when created by railsadmin由 railsadmin 创建时,布尔字段在 mongoid 查询中被忽略
【发布时间】:2014-10-01 01:50:32
【问题描述】:

我有一个叫做 Announcement 的 Mongoid 模型:

class Announcement
  include Mongoid::Document
  include Mongoid::Timestamps

  field :message
  field :starts_at, type: DateTime
  field :ends_at, type: DateTime
  field :is_permanent, type: Boolean, default: false
end

这个模型上的大部分东西似乎都可以正常工作:

Announcement.all.count

列出所有公告。

当我通过控制台创建公告时:

Announcement.create(is_permanent: true, message: "Hi", starts_at: "2014-09-30", ends_at: "2014-10-30")

Announcement.where(is_permanent: true).to_a 返回它:

[#<Announcement _id: 542b5cfa53696d0656010000, created_at: 2014-10-01 01:46:34 UTC, updated_at: 2014-10-01 01:46:34 UTC, message: "Hi", starts_at: nil, ends_at: nil, is_permanent: true>]

但是,当我通过 RailsAdmin 创建带有布尔字段的公告时,它会被放入数据库中:

[#<Announcement _id: 542b4eae53696d0552000000, created_at: 2014-10-01 00:45:34 UTC, updated_at: 2014-10-01 01:12:07 UTC, message: "Cookies!", starts_at: nil, ends_at: nil, is_permanent: "1">]

is_permanent 是"1"

因此,查询is_permanent: true 不会返回该公告。

这是 rails-admin 的错误吗?我的设置是否遗漏了什么?任何帮助/想法表示赞赏。

【问题讨论】:

    标签: ruby-on-rails mongoid rails-admin


    【解决方案1】:

    事实证明,将 Mongoid::Boolean 添加到您的模型将使 Rails Admin 插入一个 mongoid 布尔值,而不仅仅是“布尔值”。

    field :is_permanent, type: Mongoid::Boolean, default: false
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-19
      相关资源
      最近更新 更多