I have a Model Representative
class Representative< ActiveRecord::Base
validates_inclusion_of :gender, :in => Representative.genders
def self.genders
%w(M F)
end
end
When I write the validation line before the genders function definition it does not work it gives and undefined method genders error. When I write the validation below the function it works, Why is this happening? I am used to calling and defining functions without having to worry about the sequence, am I getting something really wrong here?
Thanks
Ruby executes the code line by line, like shime explains.
Therefore, at the time it run the
validates_inclusion_of, it try to find the methodgenders. It cannot find that method at that time, so it raise an error. Better to move that method up or simply define constant.Better check the backtrace in the error, and try to understand it.
Here is how you define constant so that you can access it later on by calling
Representative::GENDERS.