简体   繁体   中英

NameError: undefined method 'options' for class

When I create instance of class, Error is raised NameError: undefined method 'options' for class 'Product'.

DB: MongoDB 3.6

Class Product
     include Mongoid::Document
     include Mongoid::Timestamps
     include Mongoid::Attributes::Dynamic

     field :options, type: Hash, default: {}
end

gem: mongoid (6.1.1)

NOTE: It works well when I change field name to option or anything else, only options Fails. TIA

Taken from here :

Reserved names

If you define a field on your document that conflicts with a reserved method name in Mongoid, the configuration will raise an error. For a list of these you may look at Mongoid.destructive_fields.

Some code from mongoid:

# File 'lib/mongoid/config.rb', line 69

def destructive_fields
  Composable.prohibited_methods
end


# File 'lib/mongoid/composable.rb', line 98

def prohibited_methods
  @prohibited_methods ||= MODULES.flat_map do |mod|
    mod.instance_methods.map(&:to_sym)
  end
end

MODULES constant contains Fields module at least which defines options method. Maybe it's the reason.

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