【发布时间】:2013-06-19 19:22:30
【问题描述】:
背景:我有帖子和用户,并且都有很多社区。p>
目标:对于任何给定的用户,我想返回一个帖子集合,按帖子与用户有多少共同社区排序(共同社区越多的帖子越高上)
我当前的尝试(使用排序方法)有效:
Post.includes(:community_posts).where(community_posts: { community_id: current_user.community_ids }).sort{ |x,y| (y.community_ids & current_user.community_ids).length <=> (x.community_ids & current_user.community_ids).length }
但是有没有更好/更有效的方法来做到这一点?
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3 sorting activerecord ruby-on-rails-4