【问题标题】:Acts As Taggable On可标记的行为
【发布时间】:2013-09-10 17:39:49
【问题描述】:

我无法让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 的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 并将上下文设置为 tags,一切正常:

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

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

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 tags gem acts-as-taggable-on


    【解决方案1】:

    看起来默认实现已损坏。通过将标签明确声明为:tags 一切正常:

      acts_as_taggable_on :tags
    

    【讨论】:

      猜你喜欢
      • 2011-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-06
      • 2012-04-30
      相关资源
      最近更新 更多