我在让acts-as-taggable-on 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'))
但是,模型标记的taggings_context
都设置为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
如果我浏览了所有Taggable并将上下文设置为tags
,那么一切正常:
ActsAsTaggableOn::Tagging.all.each{|t|t.context = "tags"; t.save!}
那么为什么会这样呢?这是一个错误还是我做错了什么?
答案 0 :(得分:1)
看起来默认实现已损坏。通过明确地将标记声明为:tags
,一切正常:
acts_as_taggable_on :tags