开发者

Why aren't all tags returned?

开发者 https://www.devze.com 2023-04-02 00:29 出处:网络
Given a user model and a brand model, and acts_as_taggable_on I have 2 users that have tagged a brand with 2 tags each. Then in the console:

Given a user model and a brand model, and acts_as_taggable_on I have 2 users that have tagged a brand with 2 tags each. Then in the console:

>> 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">]

Those are the tags for Brand b only from the first user. The tags from the last user aren't showing. Why?

Edit: Using rails 3.0.9

  • Brand has_many tags, has_many brand_users and has_many users through brand_users
  • User has_many :brand_users and has_many :brands, :through => :brand_users
  • BrandUser belongs_to :brand and belongs_to :user

Below is my schema:

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"
  开发者_Python百科  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


I wasn't using the gem properly. Going through acts_as_taggable_on's core.rb file you find all_tags_on(context). Using b.tags calls all tags without a tagger (as seen in the SQL query) so using the following gives the answer for all tags with a tagger_type or tagger_id:

b.all_tags_on(:tags)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号