0

好的,我试图在这里保持简单并在 rails 控制台中进行测试。但是,我似乎无法弄清楚如何保存标签并再次读取它们。正如您从下面的粘贴中看到的那样,我无法保存@consumer 对象(带有标签)然后读出标签..

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')
 => []

此外,我在我的数据库中找不到标签或标记作为表。感谢您帮助我理解这一点。

编辑:

我想我看到了问题.. 在标记表中,我看到了 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

在此处输入图像描述

4

1 回答 1

0

尝试通过附加到 tags_list 的数组

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

然后保存

在您的情况下,acts_as_taggable_on 也会创建一个名为标签的表格。如果您没有看到它,您可能想要运行迁移。

于 2013-02-15T00:45:34.677 回答