我有一个 activerecord 模型Event
,我想按等级排序。排名将是加权属性的总和。例如,我可能想使用以下逻辑对事件进行排名:
LOG(number of followers) + (7 - number of days from now)
以下工作,但并不令人满意,因为它返回结果集而不是关系对象。因此,我将无法将其视为范围。(仅供参考,我正在使用带有 PostGIS 扩展的 Postgres)
x = Event.find_by_sql("
SELECT
(
CASE WHEN COUNT(follows.*) = 0
THEN 0
ELSE LOG(COUNT(follows.*)) END
+
SIGN(7 - (EXTRACT(EPOCH FROM start) - EXTRACT(EPOCH FROM NOW())) / 86400) * LOG(ABS(7 - (EXTRACT(EPOCH FROM start) - EXTRACT(EPOCH FROM NOW())) / 86400))
) as score,
events.*
FROM events
LEFT OUTER JOIN follows
ON events.id = follows.followable_id AND follows.followable_type = 'Event' AND follows.blocked = 'f'
WHERE (events.start > NOW()) AND (ST_DWithin(st_setsrid(st_point(#{@location[:lng]}, #{@location[:lat]}), 4326), st_transform(loc, 4326), 48280.2, true))
GROUP BY events.id
ORDER BY 1 DESC
")
我知道我可以在Events
表中添加一个计数器缓存并避免加入,但将来我会想通过其他一些关联来计算排名,所以知道如何做会很有帮助。
谢谢