acts_as_taggable对每个请求运行查询

时间:2012-09-18 08:25:18

标签: ruby-on-rails ruby-on-rails-3 acts-as-taggable-on acts-as-taggable

我有一个User.rb模型,其中包含以下行:

acts_as_taggable_on :industries, :uxes

['uxes'一词包含属于用户的用户体验偏好,例如他们是否驳回某个帮助框。]

我注意到以下查询是在每个涉及@user的请求的顶部加载的:

User Load (1.4ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 354 LIMIT 1
ActsAsTaggableOn::Tag Load (0.4ms)  SELECT `tags`.* FROM `tags` INNER JOIN `taggings` ON `tags`.id = `taggings`.tag_id WHERE ((`taggings`.taggable_id = 354) AND (`taggings`.taggable_type = 'User')) AND (taggings.context = 'uxes' AND taggings.tagger_id IS NULL)  
AREL (0.3ms)  UPDATE `users` SET `last_request_at` = '2012-09-17 09:44:24', `perishable_token` = 'un5eK7SHDNzTegt7GPUk', `updated_at` = '2012-09-17 09:44:24' WHERE `users`.`id` = 354
ActsAsTaggableOn::Tag Load (0.3ms)  SELECT `tags`.* FROM `tags` INNER JOIN `taggings` ON `tags`.id = `taggings`.tag_id WHERE ((`taggings`.taggable_id = 354) AND (`taggings`.taggable_type = 'User')) AND (taggings.context = 'uxes' AND taggings.tagger_id IS NULL)
ActsAsTaggableOn::Tagging Load (0.4ms)  SELECT `taggings`.* FROM `taggings` WHERE `taggings`.`tagger_type` IS NULL AND `taggings`.`tagger_id` IS NULL AND `taggings`.`context` = 'uxes' AND `taggings`.`tag_id` IN (NULL) AND (`taggings`.taggable_id = 354 AND `taggings`.taggable_type = 'User')

这些查询为每个加载用户的页面加载时间增加(即所有页面加载的90%以上)。为什么acts_as_taggable强制执行这些查询,除非必要,如何阻止它们加载?

1 个答案:

答案 0 :(得分:2)

ActsAsTaggableOn gem据称直接支持caching your tag lists 在您的模型中,另请参阅此question。要为标记列表启用缓存,只需在表中添加cached_tag_list列,因为缓存方法会查找名为“cached _#{context} _list”的列 在acts_as_taggable记录表上。但不确定这是否有效。