Search code examples
ruby-on-railsruby-on-rails-3validationmongoidactivemodel

Rails 3: Mongoid validates_inclusion_of error


class Request
include Mongoid::Document 
field :code, type: String      
validates :code, :presence => true, 
                 :inclusion => { :in => proc { Listing.all_codes } }

Using Mongoid, I am trying to validate the :code input on the submission form to make sure they are using a proper code that is already in the database. The listing model :code field is also named :code.

This is the error:

undefined method `all_codes' for Listing:Class

Any suggestions? What is the reference equivalent in Mongoid?


Solution

  • This is a ruby level error saying that you do not have a method looking like

    class Listing
        def self.all_codes
           # stuff
        end
    end
    

    The self. part is important.

    You might have it implemented like

    class Listing
       named_scope :all_codes, :select => #...
    end
    

    You might just want Listing.all Really the bug is a disagreement in method name between your Request class and your Listing class.