试图覆盖mongoid _id

时间:2014-09-18 16:20:32

标签: ruby mongodb mongoid

所以我试图覆盖一个类的_id字段 试过这个文档(创建自定义ID) 我有一个班级

class BlockedCreditCards
  include Mongoid::Document
  include Mongoid::Timestamps

  store_in database: 'cluster1'

  field :_id, type: String, default: ->{ cart_id } 
  field :cart_id, type: String
  field :reason, type: String

end

所以试图像这样创建一个新记录:

 BlockedCreditCards.create!({ card_id:'123123'})

结果:

Mongoid::Errors::UnknownAttribute:
Problem:
  Attempted to set a value for 'card_id' which is not allowed on the model BlockedCreditCards.
Summary:
  When setting Mongoid.allow_dynamic_fields to false and the attribute does not already exist in the attributes hash, attempting to call BlockedCreditCards#card_id= for it is not allowed. This is also triggered by passing the attribute to any method that accepts an attributes hash, and is raised instead of getting a NoMethodError.
Resolution:
  You can set Mongoid.allow_dynamic_fields to true if you expect to be writing values for undefined fields often.

0 个答案:

没有答案