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'",
Rails 3.1.3
Ruby 1.9.3
gem "mongoid", "~> 2.3.2"
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 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
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.
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.