mbleigh / acts-as-taggable-on

A tagging plugin for Rails applications that allows for custom tagging along dynamic contexts.
http://mbleigh.lighthouseapp.com/projects/10116-acts-as-taggable-on
MIT License
4.98k stars 1.19k forks source link

chained tagged_with throws ActiveRecord::StatementInvalid: Mysql2::Error: Not unique table/alias error #867

Open murtali opened 7 years ago

murtali commented 7 years ago
Activity Load (1.0ms)  SELECT `activities`.* FROM `activities` JOIN taggings activities_taggings_531c2fd  ON activities_taggings_531c2fd.taggable_id = activities.id AND activities_taggings_531c2fd.taggable_type = 'Activity' AND activities_taggings_531c2fd.tag_id = 33 AND activities_taggings_531c2fd.context = 'skills' JOIN taggings activities_taggings_a841373  ON activities_taggings_a841373.taggable_id = activities.id AND activities_taggings_a841373.taggable_type = 'Activity' AND activities_taggings_a841373.tag_id = 37 AND activities_taggings_a841373.context = 'skills' JOIN taggings activities_taggings_531c2fd  ON activities_taggings_531c2fd.taggable_id = activities.id AND activities_taggings_531c2fd.taggable_type = 'Activity' AND activities_taggings_531c2fd.tag_id = 33 AND activities_taggings_531c2fd.context = 'skills'

ActiveRecord::StatementInvalid: Mysql2::Error: Not unique table/alias: 'activities_taggings_531c2fd': SELECT `activities`.* FROM `activities` JOIN taggings activities_taggings_531c2fd  ON activities_taggings_531c2fd.taggable_id = activities.id AND activities_taggings_531c2fd.taggable_type = 'Activity' AND activities_taggings_531c2fd.tag_id = 33 AND activities_taggings_531c2fd.context = 'skills' JOIN taggings activities_taggings_a841373  ON activities_taggings_a841373.taggable_id = activities.id AND activities_taggings_a841373.taggable_type = 'Activity' AND activities_taggings_a841373.tag_id = 37 AND activities_taggings_a841373.context = 'skills' JOIN taggings activities_taggings_531c2fd  ON activities_taggings_531c2fd.taggable_id = activities.id AND activities_taggings_531c2fd.taggable_type = 'Activity' AND activities_taggings_531c2fd.tag_id = 33 AND activities_taggings_531c2fd.context = 'skills'

This occurs when chaining tagged_with and tagged_with the same tag again-- for ex: Activity.tagged_with(["problem solving", "critical thinking"], on: :skills).tagged_with("problem solving", on: :skills)

The use case is when you have a list of items already filtered by a tag and you want a count of a certain tag within that filtered group. In the use case above, you have a list of activities with the skill.

The error is recent and only started happening when we upgraded to rails 4.2x

Our solution while not optimal, has been to re-grab the records with the original tagged with and then run tagged with again on it.

quinn commented 6 years ago

I also have this same issue.

patrickdavey commented 5 years ago

Just curious, did you have this when you were using match_all? I had this issue when I was using match_all: false and changing it to any: true made the issue disappear.