Search code examples
ruby-on-railsacts-as-taggable-on

Acts As Taggable on Showing the ID instead of Name


I have 2 models, student and profile. A student can have only one profile. I set the acts_as_taggable_on :skills in the profile model.

In the partial form for new and edit action of profile, I have these line of code to add the skills (I'm using select2 library with the class of taggable):

# apps/views/profiles/_form.html.erb

<%= f.input :skill_list, input_html: { class: 'taggable', multiple: "multiple" }, collection: Profile.tag_counts_on(:skills) %>

And then in the students controller show action, I find the student:

@student = Student.find(params[:id])

In the show of student view, I can have these:

<%= @student.profile.skill_list %>

I tried to run the app, I created a new student account then created new profile, added the skills (by now, the skills form won't showed up anything because we didn't have any skills tag yet), I clicked save and it worked. The skill name list showed up in the student show page.

Then, I created another student account and created new profile to the student. By now, the skills form would shows all the skills tag that I've put in the previous profile. So, I can auto complete by selecting the available skills tag then clicked save.

However, in the student show view, I only see the skill id list not the skill name list as I expected.

What did I miss?


Solution

  • I read this How to get a list of all tags while using the gem 'acts-as-taggable-on' in Rails (not the counts) and use this code in the collection (partial form):

    ActsAsTaggableOn::Tagging.includes(:tag).where(context: 'skills').uniq.pluck(:name)
    

    and it's working. It will save the name, not the ID and can be accessed by another profile.