mbleigh/acts-as-taggable-on — Issue #220: Tag ownership and STI
Author: unindented
Created: 2012-01-17 15:31:25+00:00
State: open

I have different types of users using single table inheritance:

class User < ActiveRecord::Base
acts_as_tagger
end

class Teacher < User
end

class Student < User
end

And a node, which is taggable:

class Node < ActiveRecord::Base
acts_as_taggable
end

When I try to tag a node, the following happens:

student.tag(node, :with => :star, :on => :tags)

SQL (0.1ms) BEGIN
OupResource Load (0.2ms) SELECT `nodes`.* FROM `nodes` WHERE `nodes`.`type` = 'OupResource' AND `nodes`.`id` = 71 LIMIT 1
ActsAsTaggableOn::Tag Load (0.3ms) SELECT `tags`.* FROM `tags` WHERE (name LIKE 'star' ESCAPE '!')
SQL (1.0ms) SELECT 1 FROM `tags` WHERE (`tags`.`name` = BINARY 'star') LIMIT 1
SQL (2.0ms) describe `tags`
AREL (0.2ms) INSERT INTO `tags` (`name`) VALUES ('star')
ActsAsTaggableOn::Tag Load (0.4ms) SELECT `tags`.* FROM `tags` INNER JOIN `taggings` ON `tags`.id = `taggings`.tag_id WHERE ((`taggings`.taggable_id = 71) AND (`taggings`.taggable_type = 'Node')) AND (taggings.context = 'tags' AND
taggings.tagger_id = 8 AND
taggings.tagger_type = 'Student')
ActsAsTaggableOn::Tagging Load (0.2ms) SELECT `taggings`.* FROM `taggings` WHERE `taggings`.`taggable_id` = 71 AND `taggings`.`taggable_type` = 'Node' AND `taggings`.`tagger_type` = 'Student' AND `taggings`.`tagger_id` = 8 AND `taggings`.`tag_id` IN (NULL) AND `taggings`.`context` = 'tags'
SQL (0.3ms) SELECT 1 FROM `taggings` WHERE `taggings`.`taggable_type` = 'Node' AND `taggings`.`taggable_id` = 71 AND `taggings`.`context` = 'tags' AND `taggings`.`tagger_id` = 8 AND `taggings`.`tagger_type` = 'User' AND (`taggings`.`tag_id` = 1) LIMIT 1
SQL (1.6ms) describe `taggings`
AREL (0.3ms) INSERT INTO `taggings` (`tag_id`, `taggable_id`, `taggable_type`, `tagger_id`, `tagger_type`, `context`, `created_at`) VALUES (1, 71, 'Node', 8, 'User', 'tags', '2012-01-17 15:12:55')
SQL (0.4ms) COMMIT

As you can see in the last line, the node is tagged with `tagger_type = 'User'`.

Now if I do:

student.owned_taggings

I get the correct answer:

ActsAsTaggableOn::Tagging Load (0.3ms) SELECT `taggings`.* FROM `taggings` WHERE (`taggings`.tagger_id = 8 AND `taggings`.tagger_type = 'User')
ActsAsTaggableOn::Tag Load (0.1ms) SELECT `tags`.* FROM `tags` WHERE (`tags`.`id` = 1)
=> [#]

And if I do:

node.tags

I also get the correct answer:

ActsAsTaggableOn::Tag Load (0.3ms) SELECT `tags`.* FROM `tags` INNER JOIN `taggings` ON `tags`.id = `taggings`.tag_id WHERE ((`taggings`.taggable_id = 71) AND (`taggings`.taggable_type = 'Node') AND ((taggings.context = 'tags')))
=> [#]

But if I do:

node.tags_from(student)

I get nothing:

ActsAsTaggableOn::Tag Load (0.3ms) SELECT `tags`.* FROM `tags` INNER JOIN `taggings` ON `tags`.id = `taggings`.tag_id WHERE ((`taggings`.taggable_id = 71) AND (`taggings`.taggable_type = 'Node')) AND (taggings.context = 'tags' AND
taggings.tagger_id = 8 AND
taggings.tagger_type = 'Student')
=> []

Same if I do:

node.owner_tags_on(student, :tags)

I get nothing:

ActsAsTaggableOn::Tag Load (0.4ms) SELECT `tags`.* FROM `tags` INNER JOIN `taggings` ON `tags`.id = `taggings`.tag_id WHERE ((`taggings`.taggable_id = 71) AND (`taggings`.taggable_type = 'Node')) AND (taggings.context = 'tags' AND
taggings.tagger_id = 8 AND
taggings.tagger_type = 'Student')
=> []

Looks like it's querying with `tagger_type = 'Student'`, even though it saved it with `tagger_type = 'User'`.

What can I do to solve this?