validation failing (can't be blank) on a field that has a value

1,528 views
Skip to first unread message

Justin Collum

unread,
Dec 8, 2011, 10:20:51 PM12/8/11
to Mongoid
I have this chunk of code:
notif.needs_send = false
notif.last_send = DateTime.now
@logger.debug "notif before save: #{notif.inspect}"
notif.save!

When I run this, it fails on the last line. The exception is
"Validation failed - Needs send can't be blank". That field is very
important so I really do want the validation to be there. As you can
see from the output below, needs_send is indeed set before the save.
Is this a bug? I don't see what I could be doing wrong.

I read somewhere that an issue with en.yml could cause this. Figured
it was worth a shot so I emptied that file. That caused a different
error (about expecting a hash). Don't think that will work, plus it
was a longshot.

Here's the field declaration:
field :needs_send, type: Boolean, :default => true

notif before save: #<notification _id: 4ee16872e8c1c92d0c000002,
_type: nil, last_send: 2011-12-09 03:01:00 UTC, needs_send: false,
created: 2011-12-09 01:45:58 UTC, deleted: nil, send_attempts: 5 >

Failed to save notification, exception: Validation failed - Needs send
can't be blank. backtrace: ["D:/Ruby193/lib/ruby/gems/1.9.1/gems/
mongoid-2.3.4/lib/mongoid/persistence.rb:247:in `fail_validate!'", "D:/
Ruby193/lib/ruby/gems/1.9.1/gems/mongoid-2.3.4/lib/mongoid/
persistence.rb:71:in `save!'", "D:/Dev/AquaticKodiak/lib/
notifications_processer.rb:48:in `block (2 levels) in perform'", "D:/
Ruby193/lib/ruby/gems/1.9.1/gems/mongoid-2.3.4/lib/mongoid/contexts/
enumerable.rb:137:in `each'", "D:/Ruby193/lib/ruby/gems/1.9.1/gems/
mongoid-2.3.4/lib/mongoid/contexts/enumerable.rb:137:in `iterate'",
"D:/Ruby193/lib/ruby/gems/1.9.1/gems/mongoid-2.3.4/lib/mongoid/
criteria.rb:143:in `block in each'", "D:/Ruby193/lib/ruby/gems/1.9.1/
gems/mongoid-2.3.4/lib/mongoid/criteria.rb:143:in `tap'", "D:/Ruby193/
lib/ruby/gems/1.9.1/gems/mongoid-2.3.4/lib/mongoid/criteria.rb:143:in
`each'", "D:/Dev/AquaticKodiak/lib/notifications_processer.rb:19:in
`block in perform'", "D:/Ruby193/lib/ruby/gems/1.9.1/gems/
mongoid-2.3.4/lib/mongoid/contexts/mongo.rb:212:in `block in
iterate'",

Justin Collum

unread,
Dec 8, 2011, 10:23:08 PM12/8/11
to Mongoid
Forgot:

Rails 3.1.3
Ruby 1.9.3
gem "mongoid", "~> 2.3.2"

Jason Sydes

unread,
Dec 8, 2011, 10:50:32 PM12/8/11
to mon...@googlegroups.com
On Thu, Dec 8, 2011 at 7:20 PM, Justin Collum <jco...@gmail.com> wrote:
Here's the field declaration:
   field :needs_send, type: Boolean, :default => true

notif before save: #<notification _id: 4ee16872e8c1c92d0c000002,
_type: nil, last_send: 2011-12-09 03:01:00 UTC, needs_send: false,
created: 2011-12-09 01:45:58 UTC, deleted: nil, send_attempts: 5 >

Failed to save notification, exception: Validation failed - Needs send
can't be blank.


I just ran into this myself.  Turns out Boolean validations need to be handled a little bit differently than other fields.  I'm assuming you have something like this:

validates :needs_send,
      :presence => true, :allow_blank => false, :allow_nil => false

Try this instead:

validates :needs_send,
      :inclusion => {:in => [true, false], :message => 'must be true or false'}

Here's where I found the answer:


Hope that works!
Jason

Justin Collum

unread,
Dec 9, 2011, 12:03:34 PM12/9/11
to Mongoid
Thanks, that did help. I had to do it differently though.

I tried this:

validates :needs_send, :inclusion => {:in => [true, false],

:message => 'Needs send must be true or false'}

But still got the must not be blank error because that field was also
in the validates_presence_of list. These don't seem mutually exclusive
to me: presence should return true regardless of the value, while that
inclusion validate should be a little more fine grained. I changed
over the inclusion to a more mongoid way of doing it (below).

This:

validates_inclusion_of :needs_send, in: [true, false]
validates_presence_of :needs_send

Caused the "can't be blank error". Removing the second line allowed
the record to validate correctly. To me, that looks like a bug. Those
two validations should be able to live together. At the least I should
get a message about using "validates_presence_of" with Boolean fields
if that won't work. There's nothing on the docs page about that.

So the solution is:

validates_inclusion_of :needs_send, in: [true, false]

with :needs_send not being in the list of validates_presence_of.


On Dec 8, 7:50 pm, Jason Sydes <jason.sy...@gmail.com> wrote:


> On Thu, Dec 8, 2011 at 7:20 PM, Justin Collum <jcol...@gmail.com> wrote:
> > Here's the field declaration:
> >    field :needs_send, type: Boolean, :default => true
>
> > notif before save: #<notification _id: 4ee16872e8c1c92d0c000002,
> > _type: nil, last_send: 2011-12-09 03:01:00 UTC, needs_send: false,
> > created: 2011-12-09 01:45:58 UTC, deleted: nil, send_attempts: 5 >
>
> > Failed to save notification, exception: Validation failed - Needs send
> > can't be blank.
>
> I just ran into this myself.  Turns out Boolean validations need to be
> handled a little bit differently than other fields.  I'm assuming you have
> something like this:
>
> validates :needs_send,
>       :presence => true, :allow_blank => false, :allow_nil => false
>
> Try this instead:
>
> validates :needs_send,
>       :inclusion => {:in => [true, false], :message => 'must be true or
> false'}
>
> Here's where I found the answer:
>

> http://stackoverflow.com/questions/3608076/rails-how-do-i-validate-th...http://stackoverflow.com/questions/3648619/validating-boolean-value-i...
>
> Hope that works!
> Jason

Jason Sydes

unread,
Dec 10, 2011, 3:52:49 PM12/10/11
to mon...@googlegroups.com
On Fri, Dec 9, 2011 at 9:03 AM, Justin Collum <jco...@gmail.com> wrote:
This:

 validates_inclusion_of :needs_send, in: [true, false]
 validates_presence_of :needs_send

Caused the "can't be blank error". Removing the second line allowed
the record to validate correctly. To me, that looks like a bug. Those
two validations should be able to live together. At the least I should
get a message about using "validates_presence_of" with Boolean fields
if that won't work. There's nothing on the docs page about that.

So the solution is:

 validates_inclusion_of :needs_send, in: [true, false]

with :needs_send not being in the list of validates_presence_of.


Right.  The presence_of validation causes an error when the field is 'false' (but not when it's 'true').  The inclusion_of validation serves as a substitute for the presence_of validation for Booleans.

Glad it worked!

Jason

Jason Sydes

unread,
Dec 10, 2011, 4:00:51 PM12/10/11
to mon...@googlegroups.com
On Fri, Dec 9, 2011 at 9:03 AM, Justin Collum <jco...@gmail.com> wrote:
At the least I should
get a message about using "validates_presence_of" with Boolean fields
if that won't work. There's nothing on the docs page about that.


Yes, the docs being updated would probably help, as this behavior is not really expected.  This is actually an ActiveModel::Validations problem (which Mongoid includes to provide what looks like most of its validations).

Jason

Reply all
Reply to author
Forward
0 new messages