0
我只是想知道在我調用sort_by total_like方法之後,通過在自定義方法欄中降序來執行偏移限制和排序。 這是我的模型自定義方法rails sort_by方法在降序和偏移限制
class Post < ApplicationRecord
def total_like
self.likeposts.count
end
... more code ...
,這是劇本時,我按降序嘗試那種通過調用後total_like
if [email protected]_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'和'likeposts'之間的關係是如何定義的以及表名是如何命名的? – spickermann