1

我在让作为可标记的 gem正常工作时遇到问题。

我有一个模型:

class Resource < ActiveRecord::Base
  acts_as_taggable

end

我可以向它添加标签,尽管它tag_list已被填充:

$ a = ArchiveResource.new()
$ a.tag_list = ["one", "two", "three"]
$ a.tag_list # ["one", "two", "three"]

但是,它的tag关联不是:

$ a.tags # []

如果我检查所有标签,我可以看到它们正在被创建:

$ ActsAsTaggableOn::Tag.all #<ActsAsTaggableOn::Tag id: 1, name: "one">,
                            #<ActsAsTaggableOn::Tag id: 2, name: "two">,
                            #<ActsAsTaggableOn::Tag id: 3, name: "three">

如果我检查模型的taggings关联,我可以看到它们存在于那里:

$ a.taggings #<ActsAsTaggableOn::Tag id: 1, name: "one">,
             #<ActsAsTaggableOn::Tag id: 2, name: "two">,
             #<ActsAsTaggableOn::Tag id: 3, name: "three">

更仔细地查看对我的调用,a.tags.all我可以从查询中看到不匹配:

  ActsAsTaggableOn::Tag Load (0.9ms)  SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = 1 AND "taggings"."taggable_type" = 'ArchiveResource' AND (taggings.context = ('tags'))

但是模型的Taggingstaggings_context都设置为 single ,所以查询总是失败:tag

<ActsAsTaggableOn::Tagging id: 1, tag_id: 1, taggable_id: 1, taggable_type: "Resource", tagger_id: nil, tagger_type: nil, context: "tag", created_at: "2013-09-10 17:12:20
<ActsAsTaggableOn::Tagging id: 2, tag_id: 1, taggable_id: 1, taggable_type: "Resource", tagger_id: nil, tagger_type: nil, context: "tag", created_at: "2013-09-10 17:12:20
<ActsAsTaggableOn::Tagging id: 3, tag_id: 1, taggable_id: 1, taggable_type: "Resource", tagger_id: nil, tagger_type: nil, context: "tag", created_at: "2013-09-10 17:12:20

如果我遍历所有 Taggables 并将上下文设置为tags,一切正常:

ActsAsTaggableOn::Tagging.all.each{|t|t.context = "tags"; t.save!}

那么为什么会发生这种情况。这是一个错误还是我做错了什么?

4

1 回答 1

1

看起来默认实现被破坏了。通过明确声明标签:tags一切正常:

  acts_as_taggable_on :tags
于 2013-09-11T08:18:37.877 回答