我正在使用act-as-taggable-on(3.0.1),我在控制台上尝试了一些代码:
irb(main):005:0* Noticia.last.tags
=> [#<ActsAsTaggableOn::Tag id: 9159, name: "oil">]
irb(main):006:0> Noticia.tagged_with("oil")
=> []
我注意到我的第二个查询在sql上没有添加JOIN:
Noticia Load (0.5ms) SELECT "noticia".* FROM "noticia" WHERE (deleted_at IS NULL) ORDER BY noticia.id_noticia DESC LIMIT 1
ActsAsTaggableOn::Tag Load (0.5ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags".id = "taggings".tag_id WHERE (("taggings".taggable_id = 10630) AND ("taggings".taggable_type = 'Noticia') AND ((taggings.context = ('tags'))))
ActsAsTaggableOn::Tag Load (3.9ms) SELECT "tags".* FROM "tags" WHERE (lower(name) = 'oil')
Noticia Load (0.3ms) SELECT "noticia".* FROM "noticia" WHERE (deleted_at IS NULL) AND (1 = 0)
我不知道问题是什么,在act-as-taggable-on doc中说我只是运行'Model.tagged_with(“my_tag”)'。我按照这些步骤配置了act-as-taggable-on
答案 0 :(得分:0)
我有类似的问题:
> post = Post.create(body: "Hello world")
=> <Post id: 9, title: nil, body: "Hello world", author_id: nil, author_type: nil, created_at: "2015-10-07 08:53:32", updated_at: "2015-10-07 08:53:32">
> post.tag_list.add('hello', 'world')
=> ["hello", "world"]
> Post.tagged_with('hello')
=> <ActiveRecord::Relation []>
> Post.tagged_with('world')
=> <ActiveRecord::Relation []>
我的错误是我在发布标签列表后忘记保存我的帖子:)
> post.save
=> true
> Post.tagged_with('hello')
=> <ActiveRecord::Relation [#<Post id: 9, title: nil, body: "Hello world", author_id: nil, author_type: nil, created_at: "2015-10-07 08:53:32", updated_at: "2015-10-07 08:53:32">]>