ActsAsTaggableOn:标记不在DB中持久存在

时间:2015-12-14 22:52:46

标签: ruby-on-rails acts-as-taggable-on

我正在使用acts_as_taggable_on(3.4)插件来标记Rails(4.2.4)。我尝试通过种子文件和控制台添加自定义标签,虽然它似乎添加了属性,但我无法访问它们。

我的模特:

class Recipe < ActiveRecord::Base
  acts_as_taggable_on :tags
  acts_as_taggable_on :dietaries, :meals, :cuisines, :sources
end

种子文件:

tarte = Recipe.create(title: "Caramelized Tomato Tarte Tatin", url: "www.chocolateandzucchini.com", notes: "Lorem ipsum", favorite: false)

tarte.dietary_list.add("vegetarian," "vegan")
tarte.meal_list.add("appetizers", "mains", "dinner")
tarte.cuisine_list.add("French")
tarte.source_list.add("Chocolate and Zucchini")

控制台步骤(运行种子以在上面的种子文件中创建配方):

tarte = Recipe.first
tarte.dietary_list.add("vegetarian," "vegan")
tarte.meal_list.add("appetizers", "mains", "dinner")
tarte.cuisine_list.add("French")
tarte.source_list.add("Chocolate and Zucchini")

当我调用Recipe.first.dietary_list时,它会运行查询

Recipe Load (0.6ms)  SELECT  "recipes".* FROM "recipes"  ORDER BY "recipes"."id" ASC LIMIT 1
ActsAsTaggableOn::Tag Load (0.7ms)SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = $1 AND "taggings"."taggable_type" = $2 AND (taggings.context = 'dietaries' AND taggings.tagger_id IS NULL)  [["taggable_id", 1], ["taggable_type", "Recipe"]]

但它返回一个空数组:

=> []

如果我调用Recipe.first.dietaries,它将返回一个空的Collection Proxy:

Recipe Load (0.6ms)  SELECT  "recipes".* FROM "recipes"  ORDER BY "recipes"."id" ASC LIMIT 1
ActsAsTaggableOn::Tag Load (0.5ms)  SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = $1 AND "taggings"."taggable_type" = $2 AND "taggings"."context" = $3  [["taggable_id", 1], ["taggable_type", "Recipe"], ["context", "dietaries"]]
=> #<ActiveRecord::Associations::CollectionProxy []>

使用这个工具有什么我缺席的吗?或者,那里有更好的标记工具吗?

1 个答案:

答案 0 :(得分:0)

输入所有标签后通过调用.save解决