I have in my model
class Account < ActiveRecord::Base
validates_length_of :amount, :in 1..255, :on => update, :if => Proc.new { |a| false if a.id == nil;a.amount.blank? }
validates_length_of :name, :in 1..255, :on => update, :if => Proc.new { |a| false if a.id == nil;a.name.blank? }, :unless => user_has_amount?
end
when I comment out the if condition, it works fine but with them validation fails which is confusing. I know that the validation should only run if the proc returns true or unless returns false in my controller I have
@account.update_attributes({:name => "king", :amount => "1223"}
the save fails and when I check errors I get
@account.errors.details
{:name =>[{:error=>:too_short, :count=>1}], :amount =>[{:error=>:too_short, :count=>1}]}
Strong Params are not an issue because I have def self.params params.require(:account).permit! end
I have no idea why it fails though the value are present and correct.
schema.rbfor accounts table - Nikita Misharinvalidates :amount, length: { in: 6..20 }, on: :update. instead of validates_length_of. I've found it in rails documentation : Validations Helpers -> Length - Diego Patricio Aguilef Sánchez