act-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
singularに設定されているため、クエリは常に失敗します。
<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 を実行して context を に設定するとtags
、すべてが機能します。
ActsAsTaggableOn::Tagging.all.each{|t|t.context = "tags"; t.save!}
では、なぜこれが起こっているのでしょうか。それはバグですか、それとも何か間違ったことをしていますか?