I'm not sure what the best way to implement this is--basically I would like to check if my :start_amount field of my model is within a range from 0 to user.amount. I'm not sure how to say 'user.amount' since it's not static like the other validations.
(for reference I did
validates_numericality_of :start_amount, :allow_nil => false, :greater_than => 0
and then did this in the same model:
def validates
self.errors.add(:start_amount, "is out of range") unless self.amount_in_allowed_range
end
def amount_in_allowed_range
start_amount <= user.amount
end
It's pretty simple so I didn't think creating another class for the validation was needed (maybe that's the only way it works?)
It was working ok except when I left start_amount blank, in which case it was comparing nil <= user.amount and gave an error (which I think is weird, since it should be checking for that in the validates_numericality_of--but I guess the custom validation runs first). Then I changed it to the above with :allow_nil => false and also set it as :null => false, :default => 0 in my migrations. now it just doesn't seem to check at all? or it's permanently 0, and thus always passes validation...)
I'm really new to rails and would appreciate any help. Thanks!