按上下文获取所有标签以作为可标记行为 [英] Get all tags by context for acts-as-taggable-on

查看:47
本文介绍了按上下文获取所有标签以作为可标记行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用 https://github.com/mbleigh/acts-as-taggable-on 对于我们的 Rails 应用程序,我们遇到了问题.

We use https://github.com/mbleigh/acts-as-taggable-on for our Rails app and we've encountered a problem.

我们根据推荐(Acts-as-taggable-on 按上下文查找所有标签) Model.tag_context_counts,但这会产生非常低效的查询.随着我们网站的发展,这正成为一个问题.

We tell users the possible tags for a given model and context based on the recommended (Acts-as-taggable-on find all tags by context) Model.tag_context_counts, but this is generating horribly inefficient queries. As our site is growing this is becoming a problem.

出于某种原因,它产生了这种东西:

For some reason it generates this kind of thing:

SELECT recipes.id FROM "recipes" ORDER BY sort_order
SELECT tags.*, taggings.tags_count AS count FROM "tags" JOIN (SELECT taggings.tag_id, COUNT(taggings.tag_id) AS tags_count FROM "taggings" INNER JOIN example ON example.id = taggings.taggable_id WHERE (taggings.taggable_type = 'Example' AND taggings.context = 'example_context') AND (taggings.taggable_id IN(41,98,92,57,100,93,60,101,59,134,139,224,204,21,158,238,228,251,82,160,146,80,136,182,221,66,171,62,216,76,159,86,87,126,153,218,227,189,242,142,112,3,14,61,156,131,52,13,46,135,44,174,138,81,68,33,120,114,125,94,104,50,252,130,106,40,151,212,16,229,38,192,233,65,183,37,129,214,202,193,200,47,245,165,132,63,25,75,35,198,217,111,56,23,117,78,58,127,90,39,190,211,210,145,173,180,167,48,157,222,140,215,74,150,103,83,53,199,88,176,209,161,17,170,128,144,257,99,191,234,8,15,223,177,91,179,19,232,187,69,163,107,184,237,89,195,235,241,26,246,231,175,225,250,95,70,172,168,206,154,77,9,208,2,240,73,36,84,118,201,249,4,239,236,119,185,124,254,253,27,85,162,123,148,121,244,181,32,220,141,186,116,149,248,230,20,11,110,67,22,28,122,203,113,178,31,152,18,42,115,143,205,72,10,49,108,137,166,55,188,196,147,7,243,97,155,194,1,207,6,51,12,102,219,64,5,197,105,54,96,29,109,30,164,226,71,24,133,213,247,79,255,258,256,169)) GROUP BY taggings.tag_id HAVING COUNT(taggings.tag_id) > 0) AS taggings ON taggings.tag_id = tags.id

有没有办法在不进入原始 SQL 的情况下通过上下文获取标签(例如 SELECT DISTINCT taggings.id FROM taggings WHERE context = 'example_context')

Is there a way to get tags by context without going into raw SQL (e.g. SELECT DISTINCT taggings.id FROM taggings WHERE context = 'example_context')

推荐答案

这最终产生了更高效的查询:

This ended up generating MUCH more efficient queries:

Tagging.where(:context => context).joins(:tag).select('DISTINCT tags.name').map{ |x| x.name}

这篇关于按上下文获取所有标签以作为可标记行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆