【发布时间】:2017-08-13 10:12:29
【问题描述】:
我只是想知道在我调用 sort_by total_like 方法之后,通过在我的自定义方法 rails 中降序来进行偏移限制和排序。 这是我在模型中的自定义方法
class Post < ApplicationRecord
def total_like
self.likeposts.count
end
... more code ...
这是当我尝试调用按降序排序的 post 时的脚本
if POST==@current_entity
@entities=[]
@entity.where(paramshash).sort_by(&:total_like).reverse!.each.with_index do |entity, index|
if index >= params[:offset].to_i && index <= params[:offset].to_i + params[:limit].to_i
@entities << entity
end
end
render json: @entities
任何人都可以让它变得简单,并且不需要调用我的 Post 表中的所有记录:(
【问题讨论】:
-
能否请您发布
post和likeposts之间的关联是如何定义的以及表是如何命名的?
标签: ruby-on-rails ruby rails-activerecord ruby-on-rails-5