我需要在Rails中创建和分页列表的帮助。我有三门课:
class User < ActiveRecord::Base
has_many :rankings, :dependent => :destroy
#...
# has id, name, email, etc.
end
class Ranking < ActiveRecord::Base
belongs_to :essay
belongs_to :user
#...
#has comment, score, essay_id, user_id
end
class Essay < ActiveRecord::Base
has_many :rankings, :dependent => :destroy
#...
#has id, body, author, etc.
end排名类被创建为对每篇文章的评审,然后得到相应的essay_id和user_id设置,因此我们知道谁对哪篇文章进行排序。
现在,在应用程序的主页上,我想显示一个分页的列表,列出用户尚未对其进行排名的所有文章。我想把current_user 没有翻阅的所有文章作为的列表。对于所有的文章,我简单的做@essays = Essay.paginate(:page => params[:page]),但我想要一个特定的子集。我需要创建一组没有排名的文章吗?
发布于 2011-06-02 00:06:59
这是一个建议,不使用联接。
首先,获取用户排名的所有论文ids。
essay_ids = User.first.essay_ids然后获取所有不包含这些ids的文章;)
@essays = Essay.where('id NOT IN (?)', essay_ids + [0])我喜欢添加额外的0,这样空数组就不会破坏查询。
你可以把它放在文章的范围里
scope :not_ranked_by_user, lambda {|user| where('id NOT IN (?)', user.essay_ids + [0]) }https://stackoverflow.com/questions/6208807
复制相似问题