I would like to include a class method as my options when using the inclusion validation:
class Search < ActiveRecord::Base
  attribute :foo, Array
  validates :foo, inclusion: class_method_options
  def self.class_method_options
   ['foo', 'bar']
  end
end
However I get undefined method 'class_method_options' for Search:Class (NoMethodError).
I tried Googling for the solution, but just found how to create a custom validation. I don't need a whole new validation, I just want to use the standard Rails inclusion validator. How can I access class_method_options from the inclusion validation?
It's just not defined yet.
class Search < ActiveRecord::Base
  def self.class_method_options
   ['foo', 'bar']
  end
  attribute :foo, Array
  validates :foo, inclusion: class_method_options
end
That would work or you can do:
class Search < ActiveRecord::Base
  class_method_options = ['foo', 'bar']
  attribute :foo, Array
  validates :foo, inclusion: class_method_options
end
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With