Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validates greater than existing column in rails model

I want to validate on update that :budget_price is always greater than previous value existing in this column.

I make a check on client side by html5 but how I validate it in model.rb

I write

validates :budget_price, :numericality => { :greater_than => :budget_price }, :on => :update

But it doesn't work properly

like image 498
Haseeb Ahmad Avatar asked Jan 07 '23 01:01

Haseeb Ahmad


1 Answers

Change your second :budget_price to :budget_price_was like

validates :budget_price, :numericality => { :greater_than => :budget_price_was }, :on => :update

About other methods, see also http://api.rubyonrails.org/classes/ActiveModel/Dirty.html

like image 71
Van Huy Avatar answered Jan 21 '23 12:01

Van Huy