简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM