Ruby on rails ActiveAdmin按关联对象的属性和排序
在我使用Postgres数据库的Ruby on Rails应用程序中,我有以下模型:Ruby on rails ActiveAdmin按关联对象的属性和排序,ruby-on-rails,activeadmin,Ruby On Rails,Activeadmin,在我使用Postgres数据库的Ruby on Rails应用程序中,我有以下模型: class Sale < ApplicationRecord has_many :line_items end class LineItem < ApplicationRecord belongs_to :sale end 第二,我想对本栏进行排序-可能吗?我对上述案例的建议是,您只需根据销售id分组,然后将价格相加,然后按照下面的方法进行排序,反向排序将获得降序 LineItem.gr
class Sale < ApplicationRecord
has_many :line_items
end
class LineItem < ApplicationRecord
belongs_to :sale
end
第二,我想对本栏进行排序-可能吗?我对上述案例的建议是,您只需根据销售id分组,然后将价格相加,然后按照下面的方法进行排序,反向排序将获得降序
LineItem.group(:sale_id).sum(:price).sort_by{|k, v| v}.reverse
如果您可能需要前十名,您可以使用第一(10)
试着把价格加起来。这一原则仍应适用。类似的提示和技巧的链接可以在上找到
LineItem.group(:sale_id).sum(:price).sort_by{|k, v| v}.reverse
LineItem.group(:sale_id).sum(:price).sort_by{|k, v| v}.reverse.first(10)