鉴于用户模型和品牌模型以及acts_as_taggable_on,我有2个用户标记了品牌,每个品牌都有2个标签。然后在控制台中:
>> b = Brand.last
=> #<Brand id: 37, name: "herp", user_id: nil>
>> b.users.count
=> 2
>> b.tags
=> [#<ActsAsTaggableOn::Tag id: 24, name: "firsttag">, #<ActsAsTaggableOn::Tag id: 25, name: "lasttag">]
这些只是来自第一个用户的品牌b的标签。最后一个用户的标签未显示。为什么呢?
编辑:使用rails 3.0.9
以下是我的架构:
ActiveRecord::Schema.define(:version => 20110824083919) do
create_table "brand_users", :force => true do |t|
t.integer "brand_id"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "brands", :force => true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
t.string "user_id"
end
create_table "taggings", :force => true do |t|
t.integer "tag_id"
t.integer "taggable_id"
t.string "taggable_type"
t.integer "tagger_id"
t.string "tagger_type"
t.string "context"
t.datetime "created_at"
end
add_index "taggings", ["tag_id"], :name => "index_taggings_on_tag_id"
add_index "taggings", ["taggable_id", "taggable_type", "context"], :name => "index_taggings_on_taggable_id_and_taggable_type_and_context"
create_table "tags", :force => true do |t|
t.string "name"
end
create_table "users", :force => true do |t|
t.string "provider"
t.string "uid"
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
end
答案 0 :(得分:0)
我没有正确使用宝石。通过acts_as_taggable_on的core.rb文件,您可以找到all_tags_on(context)。使用b.tags调用没有标记器的所有标记(如SQL查询中所示),因此使用以下内容给出了tagger_type或tagger_id的所有标记的答案:
b.all_tags_on(:tags)