If I have an ActiveRecord model as follows
class Foo < ActiveRecord::Base validates_inclusion_of :value, :in => self.allowed_types def self.allowed_types # some code that returns an enumerable end end
This does not work because the allow_types method was not defined at the time the validation was evaluated. All the fixes that I can think of are mainly related to moving the method definition above the check so that it is available when necessary.
I understand that this may be more a question of coding style than anything (I want all my checks to be at the top of the model and methods at the bottom), but I believe there should be some solution for this, maybe lazy model bootstrap assessment?
- is that what I want to do is even possible? Should I just define a method above validation or is there a better solution for validation in order to achieve what I want.
source share