2013-02-15 225 views
0

好的,我试图保持简单的事情,并在轨道控制台测试..但是,我似乎无法弄清楚如何保存标签并再次读出它们。正如你可以从下面的贴看,我救不了@consumer对象(标签),然后读出标签..标签在哪里保存在acts_as_taggable_on

http://pastebin.com/txpuf09g

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

enter image description here

+0

您的标记表对我来说看起来很好。 – 2013-02-15 01:10:21

回答

0

尝试通过

@consumer.tag_list << "foo" 
@consumber.save 

追加到TAGS_LIST的阵列然后保存

而且acts_as_taggable_on并在创建一个名为提交您的标签案件。如果您没有看到它,您可能需要运行迁移。

+0

请参阅我的编辑.... – Abram 2013-02-15 00:55:28

+0

您的消费模式中是否有acts_as_taggable? – 2013-02-15 01:01:04

+0

是的,acts_as_taggable_on:标签 – Abram 2013-02-15 01:02:29