Rails 3: How to validate that A < B where A and B are both model attributes ?

Posted by Misha Moroshko on Stack Overflow See other posts from Stack Overflow or by Misha Moroshko
Published on 2010-12-28T09:47:55Z Indexed on 2010/12/28 9:54 UTC
Read the original article Hit count: 203

I would like to validate that customer_price >= my_price. I tried the following:

class Product < ActiveRecord::Base
  attr_accessor :my_price
  validates_numericality_of :customer_price, :greater_than_or_equal_to => my_price
  ...
end

(customer_price is a column in the Products table in the database, while my_price isn't.)

Here is the result:

NameError in ProductsController#index
undefined local variable or method `my_price' for #<Class:0x313b648>

What is the right way to do this in Rails 3 ?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about validation