Homeland 收藏能否把最新收藏放在前面?

chenge · May 09, 2014 · Last by chenge replied at May 09, 2014 · 1969 hits

目前这个实在不方便。

如果难改就算了,我用 raindrop 来收藏。

#1 楼 @huacnlee 比如我要找我最新收藏的,就很困难。

哦,我看错了,我看成收藏功能能否放到前面了... 是可以的,现在是 Bug

楼主直接投 PR 啊

顺便贴一下我的解决方法

之前没搞定是因为收藏的 topic_id 是放在 User 的一个 Array 字段里面

class User
   include Mongoid::Document
   field :favorite_topic_ids, type: Array
end

每次查询的时候是用 Topic.where(:_id.in => current_user.favorite_topic_ids).paginate 来取的,由于有分页所以不好基于数组的顺序排序。

今天想了想,其实 will_paginate 是可以直接分页 Array 类型的,于是改成了这样:

@topic_ids = @user.favorite_topic_ids.reverse.paginate(:page => params[:page], :per_page => 30)
@topics = Topic.where(:_id.in => @topic_ids)
@topics = @topics.to_a.sort do |a, b|
  @topic_ids.index(a.id) <=> @topic_ids.index(b.id)
end

#5 楼 @huacnlee :plus1: 太棒了!

You need to Sign in before reply, if you don't have an account, please Sign up first.