加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

红宝石 – 覆盖了一个Mongoid模型的设置者和吸烟者

发布时间:2020-12-16 20:00:02 所属栏目:百科 来源:网络整理
导读:有没有办法在Mongoid中覆盖模型的setter或getter?就像是: class Project include Mongoid::Document field :name,:type = String field :num_users,type: Integer,default: 0 key :name has_and_belongs_to_many :users,class_name: "User",inverse_of: :p
有没有办法在Mongoid中覆盖模型的setter或getter?就像是:
class Project
  include Mongoid::Document
  field :name,:type => String
  field :num_users,type: Integer,default: 0
  key :name
  has_and_belongs_to_many :users,class_name: "User",inverse_of: :projects

  # This will not work
  def name=(projectname)
    @name = projectname.capitalize
  end
end

在不使用虚拟字段的情况下,可以覆盖名称方法吗?

解决方法

def name=(projectname)
  self[:name] = projectname.capitalize
end

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读