Validation fails when using validates_presence_of and validates_numericality_of

I need to validate data in my model and then test this model using rspec:

Here is the validation in model:

validates_presence_of :sales_price
validates_presence_of :retail_price

validates_numericality_of :sales_price, :greater_than => 0
validates_numericality_of :retail_price,:greater_than => 0
validates_numericality_of :sales_price, :less_than => :retail_price,
                                          :message => "must be less than retail price."

when i'm trying to test tho model like this

it { should validate_presence_of :sales_price }
it { should validate_presence_of :retail_price }
it { should validate_numericality_of :sales_price }
it { should validate_numericality_of :retail_price }

I get this error

Failure/Error: it { should validate_presence_of :retail_price }
   comparison of Float with nil failed
 # ./spec/models/offer_option_spec.rb:19:in `block (2 levels) in <top (required)>'

How can i solve this?


@sled is correct that the nil check is built into validates_numericality_of and is enabled by default, so it's unnecessary to have both requirements.

One other thing to note is that sales price really is validating numericality twice which might be causing a problem. I would instead change it to

validates_numericality_of :sales_price, :greater_than => 0, :less_than => :retail_price, :message => "must be less than retail price."

Need Your Help

naturalHeight and naturalWidth property new or deprecated?

javascript dom

naturalHeight and naturalWidth property of HTMLImageElement object is not mentioned in mozilla website Link

LazyHighcharts scatter graph issue

ruby-on-rails ruby-on-rails-3.2

I'm having an issue displaying the data in my scatter high charts graph. I'm trying to pass speed as the x axis and established a the y. If i remove the variables from the f.series and place data =>

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.