NotSimon
NotSimon

Reputation: 1815

Boolean field being ignored in mongoid query when created by railsadmin

I have a Mongoid model called Announcement:

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

Most of the things on this model seem to work fine:

Announcement.all.count

lists all announcements.

When I create an announcement through the console:

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

Announcement.where(is_permanent: true).to_a returns it:

[#<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>]

However, when I create an announcement with a boolean field through the RailsAdmin, it gets put in the database:

[#<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">]

and is_permanent is "1".

As a result, querying for is_permanent: true doesn't return that Announcement.

Is this a bug with rails-admin? Did I miss something with my setup? Any help/thoughts appreciated.

Upvotes: 3

Views: 1101

Answers (1)

NotSimon
NotSimon

Reputation: 1815

Turns out that adding Mongoid::Boolean to your model will make Rails Admin insert a mongoid boolean, not just "boolean".

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

Upvotes: 2

Related Questions