【问题标题】:order active_admin column by parent item in belongs_to relationship在belongs_to 关系中按父项排序active_admin 列
【发布时间】:2015-09-17 18:32:05
【问题描述】:

我有两个模型:show_request 和 show。一个show_Request 属于一个show 和一个show has_many show_requests。在active_admin 的show_request 页面上,我想通过show 的created_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" = $1 LIMIT 1  [["id", 2]]
  User Load (0.2ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = $1 LIMIT 1  [["id", 1]]
  CACHE (0.0ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = $1 LIMIT 1  [["id", 1]]
  Show Load (0.2ms)  SELECT "shows".* FROM "shows"
  User Load (0.2ms)  SELECT "users".* FROM "users"

这不起作用。它根本不影响列的顺序。我该如何解决这个问题?

【问题讨论】:

  • Athar,我试过了,但什么也没做。完全没有变化。
  • 如果你从可排序的行中删除 _asc 部分会发生什么。改成这样column 'Show', sortable: "shows.created_at" do |show_req|
  • 谢谢,试过了,但也没有用。有趣的是,无论我写什么,都没有效果。例如,如果我写shows.created_at_fdskjfsdlkj,则没有区别。
  • hmmm 根据文档activeadmin.info/docs/3-index-pages/index-as-table.html #Associated Sorting 如果您在scoped_collection 操作中添加此super.includes :show 并在显示列中执行此操作column "Show", sortable: 'shows.created_at' 它应该可以工作,它只是尝试访问来自协会的那一列。

标签: ruby-on-rails ruby ruby-on-rails-4 activerecord activeadmin


【解决方案1】:

看看这部分日志:

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)

您可以看到ShowRequests 和Shows 在单独的查询中加载。 sortable 在这里不起作用,因为您不能在没有连接的情况下按另一个表的字段排序一个表。解决方法应该是告诉 ActiveRecord 您需要在查询中引用 shows 表:

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

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

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

【讨论】:

  • 谢谢?应该是 :shows 还是 :show? Includes 不能与 :shows 一起使用,所以我将其更改为 :show。尽管它正在更改服务器日志,但它仍然不会影响订单
  • 你说得对,includes 应该是:show,但references 仍然是:shows。
  • 应该是shows.created_at。对于引用和可排序,您引用的是表名。
  • 谢谢 ahmacleod,试过了,但没有改变,除了服务器日志现在不同了。它不会让我把我的日志放在评论中。它说它们太长了。有没有其他方法可以把它们放上去?
猜你喜欢
  • 2016-11-20
  • 2016-01-02
  • 2013-04-02
  • 1970-01-01
  • 2016-11-10
  • 2016-06-21
  • 1970-01-01
  • 1970-01-01
  • 2021-03-18
相关资源
最近更新 更多