有没有办法在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