1

我有一个User.rb包含以下行的模型:

acts_as_taggable_on :industries, :uxes

[“uxes”术语包含属于用户的 UX 偏好,例如他们是否关闭了某个帮助框。]

我注意到的是,每个涉及 a 的请求的顶部都会加载以下查询@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强制这些查询发生,除非必要,否则我如何阻止它加载它们?

4

1 回答 1

2

据称 ActsAsTaggableOngem 支持直接在模型中缓存您的标签列表,另请参阅此问题。要为标签列表启用缓存,只需cached_tag_list在表中添加一列,因为缓存方法会在acts_as_taggable记录表中查找名为“cached_#{context}_list”的列。不过,不确定这是否有效。

于 2012-09-18T08:43:27.490 回答