按 belongs_to 关系中的父项排序 active_admin 列

order active_admin column by parent item in belongs_to relationship

我有两个模型:show_requestshow。一个show_Requestbelongs_to一个show和一个showhas_manyshow_requests。在 active_admin 的 show_request 页面上,我想按 showcreated_at 值排序 show_requests。到目前为止,这是我的代码:

ActiveAdmin.register ShowRequest do

  controller do
    def scoped_collection
      end_of_association_chain.includes(:show)
      #I also tried ShowRequest.includes(:show)
    end
  end

  index do
    column 'Show', sortable: "shows.created_at_asc" do |show_req|
        link_to show_req.show.name, admin_show_path(show_req.show)
    end
  end

end

服务器日志如下:

Started GET "/admin/show_requests" for 127.0.0.1 at 2015-09-18 09:35:36 -0400
Processing by Admin::ShowRequestsController#index as HTML
  AdminUser Load (0.3ms)  SELECT  "admin_users".* FROM "admin_users"  WHERE "admin_users"."id" = 1  ORDER BY "admin_users"."id" ASC LIMIT 1
   (1.2ms)  SELECT COUNT(*) FROM "show_requests"  WHERE (not_going_to_show = 'f' OR i_want_my_horse_to_compete = 't')
   (0.2ms)  SELECT COUNT(*) FROM "show_requests"
   (0.2ms)  SELECT COUNT(*) FROM "show_requests"  WHERE (not_going_to_show = 't' AND i_want_my_horse_to_compete = 'f')
   (0.3ms)  SELECT COUNT(count_column) FROM (SELECT  1 AS count_column FROM "show_requests"  WHERE (not_going_to_show = 'f' OR i_want_my_horse_to_compete = 't') LIMIT 30 OFFSET 0) subquery_for_count
  CACHE (0.0ms)  SELECT COUNT(count_column) FROM (SELECT  1 AS count_column FROM "show_requests"  WHERE (not_going_to_show = 'f' OR i_want_my_horse_to_compete = 't') LIMIT 30 OFFSET 0) subquery_for_count
  CACHE (0.0ms)  SELECT COUNT(*) FROM "show_requests"  WHERE (not_going_to_show = 'f' OR i_want_my_horse_to_compete = 't')
  CACHE (0.0ms)  SELECT COUNT(count_column) FROM (SELECT  1 AS count_column FROM "show_requests"  WHERE (not_going_to_show = 'f' OR i_want_my_horse_to_compete = 't') LIMIT 30 OFFSET 0) subquery_for_count
  ShowRequest Load (2.0ms)  SELECT  "show_requests".* FROM "show_requests"  WHERE (not_going_to_show = 'f' OR i_want_my_horse_to_compete = 't')  ORDER BY "show_requests"."id" desc LIMIT 30 OFFSET 0
  Show Load (9.7ms)  SELECT "shows".* FROM "shows"  WHERE "shows"."id" IN (2, 1)
  User Load (0.4ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" =  LIMIT 1  [["id", 2]]
  User Load (0.2ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" =  LIMIT 1  [["id", 1]]
  CACHE (0.0ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" =  LIMIT 1  [["id", 1]]
  Show Load (0.2ms)  SELECT "shows".* FROM "shows"
  User Load (0.2ms)  SELECT "users".* FROM "users"

这是行不通的。它根本不影响列的顺序。我该如何解决这个问题?

看看这部分日志:

ShowRequest Load (2.0ms)  SELECT  "show_requests".* FROM "show_requests"  WHERE (not_going_to_show = 'f' OR i_want_my_horse_to_compete = 't')  ORDER BY "show_requests"."id" desc LIMIT 30 OFFSET 0
Show Load (9.7ms)  SELECT "shows".* FROM "shows"  WHERE "shows"."id" IN (2, 1)

您可以看到 ShowRequestShow 是在单独的查询中加载的。 sortable 不会在这里工作,因为你不能在没有连接的情况下按另一个字段订购一个 table。解决方法应该是告诉 ActiveRecord 您需要在查询中引用 shows table:

controller do
  def scoped_collection
    super.includes(:show).references(:shows)
  end
end

index do
  column :show, sortable: 'shows.created_at'
end

references 仅适用于 includes,并强制 Rails 在预先加载时执行连接。