ruby-on-railsrubyacts-as-taggable-on

ActsAsTaggableOn add tag to object


I'm working on a legacy project that uses ActsAsTaggableOn, but it looks (based on the documentation) that it isn't using it normally?

my Reports object has several ActsAsTaggableOn objects, year_tags, for example.

I'm trying to add a tag, and based on: https://rubydoc.info/gems/acts-as-taggable-on/#usage I should be able to just use :

report.year_tags.add("foo")

However, that says:

NoMethodError: undefined method `add' for #<ActsAsTaggableOn::Tag::ActiveRecord_Associations_CollectionProxy:0x00007fbedb9134e8>

I can list year_tags and get the length, and when I output them, I get strings as expected (printed one per line, but maybe that's normal for Ruby?)

I year_tags DOES have a push method but that wants an ActsAsTaggableOn object, not a string... which seems weird.

ETA:

a bit more detail, having spent more time looking at this:

It appears that the typical way of using ActsAsTaggableOn is to have a single tag_list, however, we have several distinct types of tags, so we have report.year_tags, and report.level_tags, etc... Each one appears to be a, maybe iterator? on a list of ActsAsTaggableOn::Tag objects? or something like that?


Solution

  • you can use acts_as_taggable_on e.g. acts_as_taggable_on :year_tags then you'll be able to do year_tags_list.add "tag" see source code as reference