3

Im using acts-as-taggable-on (3.0.1) and I tried some code on console:

irb(main):005:0* Noticia.last.tags
=> [#<ActsAsTaggableOn::Tag id: 9159, name: "oil">]
irb(main):006:0> Noticia.tagged_with("oil")
=> []

I noticed that my second query dont add a JOIN at sql:

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)

I don't know what's the problem, In acts-as-taggable-on doc say I just run 'Model.tagged_with("my_tag")'. I followed these step to configure acts-as-taggable-on

Osny Netto
  • 562
  • 3
  • 9
  • 28

2 Answers2

1

For others who may stumble upon this question more recently. (Note: using Postgres database)

The generated migrations contain the following lines:

      t.references :taggable, polymorphic: true
      t.references :tagger, polymorphic: true

which creates the taggings.taggable_id and taggings.tagger_id columns as type bigint. If database primary keys are some other type (e.g. string or uuid), the tag save doesn't fail but the taggings record puts 0 in the taggable_id and tagger_id columns. This effectively results in a model's tag lookup being empty despite having attempted to associate new tags with it.

To fix, adjust the migration:

      t.references :taggable, polymorphic: true, type: :uuid
      t.references :tagger, polymorphic: true, type: :uuid

There is an open Github Issue for this bug.

changingrainbows
  • 2,551
  • 1
  • 28
  • 35
0

I had similar problem:

> 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 []>

My mistake was that i simply forgot to save my post after adding tags to post's tag_list :)

> 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">]> 
chaimann
  • 193
  • 1
  • 8