0
鑑於用戶模型和品牌模型,以及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用戶通過brand_users
- 用戶的has_many:brand_users和的has_many:品牌:通過=>:brand_users
- BrandUser belongs_to的:品牌和belongs_to的:用戶
下面是我的架構:
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
類似http://stackoverflow.com/問題/ 7273768/how-can-i-display-all-tags-all-users-for-a-model-instance除了這裏,每個品牌都應該返回Brand對象而不是BrandUser對象的所有標籤。 – Simpleton