MongoDB Mongoid 中 attr_protected 错误: NoMethodError: undefined method `attr_protected' for User:Class

mingmingpao · April 09, 2015 · Last by king1990_cool replied at April 09, 2015 · 7105 hits

现在用 Mongoid4.0 了,配置

class User
    include Mongoid::Document
    field :first_name, type: String
    field :password, type: String
    attr_protected :password # focus this
end

出现了标题的错误。我想要在 model 层禁止更新密码,思路来源于 http://mongoid.org/en/mongoid/docs/documents.html#security

mongo4.0 已经取消了 attr_protected 而且 3.1.6 里面也是继承自 ActiveSupport 里面的,这个你可以自己去看 https://github.com/mongoid/mongoid/releases/tag/v3.1.6 里面 filelds 的源码 由于 activerecord3.0 之后也不提供这个方法了 http://apidock.com/rails/ActiveRecord/Base/attr_protected/class 所以你使用 4.0 的 mongoid 就会报错了,不过不知道有没有新的替代方法,用 active_record 比较少。

You need to Sign in before reply, if you don't have an account, please Sign up first.