为了保持DRY,我有一个包含Mongoid文档的ModelBase类,如下所示:
class ModelBase
include Mongoid::Document
alias_attribute :guid, :id
def as_json(options = {})
azove_hash = options.merge(:methods => :guid)
super azove_hash
end
end
然后我的所有模型都继承自ModelBase,它们似乎工作正常。但是,有一种模型我使用CarrierWave。当它继承自ModelBase时,对mount_uploader的调用失败。当我将模型包含在内部而没有子类化时,它可以正常工作。是不是可以在继承自另一个类的类中使用carrierwave?
这是失败的类的版本。会很感激任何建议/想法
require 'carrierwave/orm/mongoid'
class SomeOtherModel < ModelBase
field :abstract
validates :abstract, :presence => true
field :category
validates :category, :presence => true, :inclusion => {:in => %w{audio graphics text video}}
field :content_uri
validates :content_uri, :presence => true
has_and_belongs_to_many :topics
has_and_belongs_to_many :events
has_and_belongs_to_many :authors, :class_name => "User"
mount_uploader :content, ContentUploader
attr_accessible :abstract, :category, :content, :content_uri, :authors, :topics, :events
end
答案 0 :(得分:1)
我认为你的事情太复杂了。我认为不需要使用mongoid文件从modelbase继承。 Mongoid本身不使用继承,只需根据需要包含模块。
因此,如果您重新使用了一组字段,例如联系信息,请执行以下操作:
class Customer
include Mongoid::Document
include DataModules::ContactDocument
mounts_uploader :logo, LogoUploader
end
class User
inclue Mongoid::Document
include DataModules::ContactDocument
end
然后在/lib/data_modules/contact_document.rb中包含要重用的代码
module DataModules::ContactDocument
def self.included(receiver)
receiver.class_eval do
field :email, :type=>String
...
validates_existence_of :email
end
end
end