Rails - 创建没有数据库标记的模型

时间:2016-05-01 15:56:53

标签: ruby-on-rails acts-as-taggable-on

我有一个像这样的无表格模型:

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'。但是我试过包含ActiveModel::Model,它仍然不起作用。我可以使我的无表格模型有任何想法可以标记吗?

1 个答案:

答案 0 :(得分:0)

source code of the plugin可以看出,它要求您将其与AR一起使用。它包含在ActiveRecord::Base

所以,您可能也希望将它们添加到您的课程中,如下所示:

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需要ActiveRecord::Base类中可用的类AR特定方法,例如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"