【问题标题】:ActiveAdmin: Sort by association (belongs_to)ActiveAdmin:按关联排序(belongs_to)
【发布时间】:2012-06-29 17:42:15
【问题描述】:

我有一个索引管理视图,其中显示了关联的模型City。我希望能够按城市名称排序,但是当我单击列标题进行排序时,出现以下错误:

ActiveRecord::StatementInvalid in Admin/deals#index

SQLite3::SQLException: no such column: cities.name: SELECT  "deals".* FROM "deals"  ORDER BY cities.name desc LIMIT 30 OFFSET 0

索引视图

ActiveAdmin.register Deal do
  index do
     column :id
     column :city
  end
  ...
end

型号

class Deal < ActiveRecord::Base
  belongs_to :city
end

如何按城市排序?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3.1 activeadmin


    【解决方案1】:

    使用 Rails 3 或 4:

    index do
      column :city, :sortable=>:"cities.name"
    
      ...
    end
    

    然后,在同一个文件中,您需要这样做,以便 Deal 查询包含 City 属性:

    controller do
      def scoped_collection
        Deal.includes(:city)
      end
    
      ...
    end
    

    【讨论】:

      【解决方案2】:

      看看https://github.com/gregbell/active_admin/pull/623#issuecomment-2419393 和下面的cmets。在定义交易索引页面之前,您必须告诉 activeadmin 如何进行排序 - 可能类似于 column :city, :sortable =&gt; 'cities.name' 并定义 joined 范围。类似的东西

      scope :joined, :default => true do |deals|
        deals.includes [:city]
      end
      

      【讨论】:

      • 不幸的是,如果您尝试将它与过滤一起使用,它会刹车
      猜你喜欢
      • 1970-01-01
      • 2016-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-02
      • 2012-09-15
      相关资源
      最近更新 更多