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

mingmingpao · 2015年04月09日 · 最后由 king1990_cool 回复于 2015年04月09日 · 7105 次阅读

现在用 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 比较少。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号