Acts As Taggable On
I'm having problems getting the acts-as-taggable-on gem working properly.
I have a model:
class Resource < ActiveRecord::Base
acts_as_taggable
end
I can add tags to it, and though its tag_list
is populated:
$ a = ArchiveResource.new()
$ a.tag_list = ["one", "two", "three"]
$ a.tag_list # ["one", "two", "three"]
However, its tag
association is not:
$ a.tags # []
If I check for all the tags, I can see they are being created:
$ ActsAsTaggableOn::Tag.all #<ActsAsTaggableOn::Tag id: 1, name: "one">,
#<ActsAsTaggableOn::Tag id: 2, name: "two">,
#<ActsAsTaggableOn::Tag id: 3, name: "three">
And if I check the model's taggings
association I can see they exist there:
$ a.taggings #<ActsAsTaggableOn::Tag id: 1, name: "one">,
#<ActsAsTaggableOn::Tag id: 2, name: "two">,
#<ActsAsTaggableOn::Tag id: 3, name: "three">
Looking more closely at a call to a.tags.all
I can see from the query that there is a mismatch:
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'))
However the taggings_context
of the model's Taggings are all set to tag
singular, so the query always fails:
<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
If I run through all the Taggables and set context to tags
, everything works:
ActsAsTaggableOn::Tagging.all.each{|t|t.context = "tags"; t.save!}
So why is this happening. Is it a bug or am I doing something wrong?
Looks like the default implementation is broken. By explicitly declaring the tags as :tags
everything works OK:
acts_as_taggable_on :tags
链接地址: http://www.djcxy.com/p/35944.html
上一篇: Rails表现为Taggable
下一篇: 作为标记的行为