Validations not working?

10 views
Skip to first unread message

Avishai

unread,
Jul 13, 2011, 5:39:50 AM7/13/11
to rubyonra...@googlegroups.com
Hi,

I'm having some serious problems with my validations. I have a mongoid model set up with validations, which looks like this:

class Apartment
  include Mongoid::Document
  include Mongoid::Timestamps
  validates :price, :numericality => { :greater_than => 0, :allow_blank => false }
  field :price, :type => Integer
end

But then when I actually try to run the validations, they pass and claim the object is valid, even if I pass complete garbage. Here's the output:

ruby-1.9.2-p180 :006 > Apartment.validators_on(:price)
 => [#<ActiveModel::Validations::NumericalityValidator:0x00000102924cc8 @attributes=[:price], @options={:only_integer=>false, :allow_nil=>false, :greater_than=>0, :allow_blank=>false}>] 
ruby-1.9.2-p180 :007 > a = Apartment.new(:price => "G@I$G@GR#&GUGshkj4h53$$$*@^")
 => #<Apartment _id: 4e1d66f835be35fddb000002, _type: nil, created_at: nil, updated_at: nil, address: nil, price: "G@I$G@GR#&GUGshkj4h53$$$*@^"> 
ruby-1.9.2-p180 :008 > a.valid?
 => true 

I've tried disabling gems, commenting out and then re-adding validations one by one, but nothing seems to work. If you have any fresh direction on how to track this down, I'd really appreciate it. Thanks!

-Avishai

Bernd Ritter

unread,
Jul 13, 2011, 5:42:32 AM7/13/11
to rubyonra...@googlegroups.com
Hi,

that price looks like random stuff. Is it relevant that you call the validator before the field-definition?

Greetings,
Bernd

2011/7/13 Avishai <avisha...@gmail.com>
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/u8C4AV2SfNQJ.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Avishai Weiss

unread,
Jul 13, 2011, 5:47:45 AM7/13/11
to rubyonra...@googlegroups.com
Yes, the price is random junk that's clearly supposed to fail validation, but it passes with no errors.

I just tried it with the validations after the field definitions; still the same problem.

Cheers,

Avishai

Bernd Ritter

unread,
Jul 13, 2011, 6:32:36 AM7/13/11
to rubyonra...@googlegroups.com
Hi Avishai,

i just tried this:  

irb(main):001:0> "G@I$G@GR#&GUGshkj4h53$$$*@^".to_i
=> 0

As I don't know how the conversion would work, but if it works like that it will return 0. But it still should not be valid.Hmmm.

Bernd

2011/7/13 Avishai Weiss <avisha...@gmail.com>

Avishai Weiss

unread,
Jul 13, 2011, 6:51:23 AM7/13/11
to rubyonra...@googlegroups.com
Right, but even so, it should fail. When I try pasting my Apartment model into a fresh Rails app, it does:

ruby-1.9.2-p180 :001 > a = Apartment.new(:price => "G@I$G@GR#&GUGshkj4h53$$$*@^")
 => #<Apartment _id: 4e1d74fa35be3560e8000001, _type: nil, created_at: nil, updated_at: nil,price: "G@I$G@GR#&GUGshkj4h53$$$*@^"> 
ruby-1.9.2-p180 :002 > a.valid?
 => false 

I compared the Gemfiles of both apps, and they're identical.... This is very strange...
Reply all
Reply to author
Forward
0 new messages