好的,我試圖保持簡單的事情,並在軌道控制檯測試..但是,我似乎無法弄清楚如何保存標籤並再次讀出它們。正如你可以從下面的貼看,我救不了@consumer對象(標籤),然後讀出標籤..標籤在哪裏保存在acts_as_taggable_on
1.9.3p286 :001 > @consumer = Consumer.first
Creating scope :page. Overwriting existing method Consumer.page.
Consumer Load (1.3ms) SELECT "consumers".* FROM "consumers" LIMIT 1
=> #<Consumer id: 29, hl: nil, created_at: "2012-10-27 23:27:15", updated_at: "2012-10-27 23:27:15", user_id: 33, display_name: nil, tagline: nil, desc: nil>
1.9.3p286 :002 > @consumer.tags
ActsAsTaggableOn::Tag Load (2.3ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = 29 AND "taggings"."taggable_type" = 'Consumer' AND (taggings.context = 'tags')
=> []
1.9.3p286 :003 > @consumer.tag_list
ActsAsTaggableOn::Tag Load (1.4ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = 29 AND "taggings"."taggable_type" = 'Consumer' AND (taggings.context = 'tags' AND taggings.tagger_id IS NULL)
=> []
1.9.3p286 :004 > @consumer.tag_list = "hi, ho"
=> "hi, ho"
1.9.3p286 :005 > @consumer
=> #<Consumer id: 29, hl: nil, created_at: "2012-10-27 23:27:15", updated_at: "2012-10-27 23:27:15", user_id: 33, display_name: nil, tagline: nil, desc: nil>
1.9.3p286 :006 > @consumer.save
(0.4ms) BEGIN
(0.7ms) UPDATE "consumers" SET "updated_at" = '2013-02-15 00:25:05.187733' WHERE "consumers"."id" = 29
ActsAsTaggableOn::Tag Load (0.8ms) SELECT "tags".* FROM "tags" WHERE (lower(name) = 'hi' OR lower(name) = 'ho')
ActsAsTaggableOn::Tag Exists (0.6ms) SELECT 1 AS one FROM "tags" WHERE "tags"."name" = 'hi' LIMIT 1
SQL (1.3ms) INSERT INTO "tags" ("name") VALUES ($1) RETURNING "id" [["name", "hi"]]
ActsAsTaggableOn::Tag Exists (0.5ms) SELECT 1 AS one FROM "tags" WHERE "tags"."name" = 'ho' LIMIT 1
SQL (0.4ms) INSERT INTO "tags" ("name") VALUES ($1) RETURNING "id" [["name", "ho"]]
ActsAsTaggableOn::Tag Load (0.7ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = 29 AND "taggings"."taggable_type" = 'Consumer' AND (taggings.context = 'tags' AND taggings.tagger_id IS NULL)
ActsAsTaggableOn::Tagging Exists (0.7ms) SELECT 1 AS one FROM "taggings" WHERE ("taggings"."tag_id" = 5 AND "taggings"."taggable_type" = 'Consumer' AND "taggings"."taggable_id" = 29 AND "taggings"."context" = 'tags' AND "taggings"."tagger_id" IS NULL AND "taggings"."tagger_type" IS NULL) LIMIT 1
SQL (1.5ms) INSERT INTO "taggings" ("context", "created_at", "tag_id", "taggable_id", "taggable_type", "tagger_id", "tagger_type") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id" [["context", "tags"], ["created_at", Fri, 15 Feb 2013 00:25:05 UTC +00:00], ["tag_id", 5], ["taggable_id", 29], ["taggable_type", "Consumer"], ["tagger_id", nil], ["tagger_type", nil]]
ActsAsTaggableOn::Tagging Exists (0.7ms) SELECT 1 AS one FROM "taggings" WHERE ("taggings"."tag_id" = 6 AND "taggings"."taggable_type" = 'Consumer' AND "taggings"."taggable_id" = 29 AND "taggings"."context" = 'tags' AND "taggings"."tagger_id" IS NULL AND "taggings"."tagger_type" IS NULL) LIMIT 1
SQL (0.5ms) INSERT INTO "taggings" ("context", "created_at", "tag_id", "taggable_id", "taggable_type", "tagger_id", "tagger_type") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id" [["context", "tags"], ["created_at", Fri, 15 Feb 2013 00:25:05 UTC +00:00], ["tag_id", 6], ["taggable_id", 29], ["taggable_type", "Consumer"], ["tagger_id", nil], ["tagger_type", nil]]
(11.9ms) COMMIT
=> true
1.9.3p286 :007 > Consumer.first.tags
Consumer Load (0.9ms) SELECT "consumers".* FROM "consumers" LIMIT 1
ActsAsTaggableOn::Tag Load (1.0ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = 30 AND "taggings"."taggable_type" = 'Consumer' AND (taggings.context = 'tags')
=> []
而且,我找不到標籤或標記爲我的數據庫中的表格。感謝您幫助我理解這一點。
編輯:
我想我看到的問題..在表的Tagging我看到ID,TAG_ID,taggable_id,taggable_type,tagger_id,tagger_type,上下文列..行似乎完成tagger_id除外和tagger_type。你認爲這與標籤沒有關聯到消費者ID有什麼關係嗎?我想問題是,Consumer.id實際上應該在標記表中出現在哪裏。目前,Consumer.first.id不在標記中的任何列!該Consumer.first.id == 30
您的標記表對我來說看起來很好。 – 2013-02-15 01:10:21