给定一个用户模型和一个品牌模型,以及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
- 品牌 has_many 标签、has_many brand_users 和 has_many users 通过 brand_users
- 用户 has_many :brand_users 和 has_many :brands, :through => :brand_users
- BrandUser belongs_to :brand 和 belongs_to :user
以下是我的架构:
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