Rails - 使没有数据库标记的模型
Rails - Make model without database taggable
我有一个像这样的无表模型:
class Wiki
include ActiveModel::AttributeMethods
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
# No Database
def persisted?
false
end
# ...
end
当我在这个模型中获得 acts_as_taggable
时,我得到了 undefined local variable or method 'acts_as_taggable'
。但是我尝试了 include ActiveModel::Model
,它仍然不起作用。我有什么想法可以使我的无表模型可标记吗?
正如您从source code of the plugin中看到的,它需要您将其与 AR 一起使用。它包含自己到 ActiveRecord::Base
.
因此,您可能还想将这些添加到您的 class 中,如下所示:
class Wiki
include ActiveModel::AttributeMethods
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
# Adding `acts_as_taggable`
extend ActsAsTaggable::Taggable
include ActsAsTaggable::Tagger
# No Database
def persisted?
false
end
# ...
end
但是!
但是,话又说回来,您将必须包含更多 AR 模块才能使其正常工作。因为,gem 要求您的 class AR 特定方法在 ActiveRecord::Base
class 中可用,例如 has_many
等
对于example here:
has_many :taggings, :as => :taggable, :dependent => :destroy, :include => :tag, :class_name => "ActsAsTaggable::Extra::Tagging"
has_many :base_tags, :through => :taggings, :source => :tag, :class_name => "ActsAsTaggable::Tag"
我有一个像这样的无表模型:
class Wiki
include ActiveModel::AttributeMethods
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
# No Database
def persisted?
false
end
# ...
end
当我在这个模型中获得 acts_as_taggable
时,我得到了 undefined local variable or method 'acts_as_taggable'
。但是我尝试了 include ActiveModel::Model
,它仍然不起作用。我有什么想法可以使我的无表模型可标记吗?
正如您从source code of the plugin中看到的,它需要您将其与 AR 一起使用。它包含自己到 ActiveRecord::Base
.
因此,您可能还想将这些添加到您的 class 中,如下所示:
class Wiki
include ActiveModel::AttributeMethods
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
# Adding `acts_as_taggable`
extend ActsAsTaggable::Taggable
include ActsAsTaggable::Tagger
# No Database
def persisted?
false
end
# ...
end
但是!
但是,话又说回来,您将必须包含更多 AR 模块才能使其正常工作。因为,gem 要求您的 class AR 特定方法在 ActiveRecord::Base
class 中可用,例如 has_many
等
对于example here:
has_many :taggings, :as => :taggable, :dependent => :destroy, :include => :tag, :class_name => "ActsAsTaggable::Extra::Tagging"
has_many :base_tags, :through => :taggings, :source => :tag, :class_name => "ActsAsTaggable::Tag"