简体   繁体   English

如何在active_admin中包括关联的模型

[英]How to includes associated models in active_admin

In a Rails application, I have two models like this : 在Rails应用程序中,我有两个这样的模型:

class Painting < ActiveRecord::Base
  belongs_to :artist
end

class Artist < ActiveRecord::Base
  belongs_to :country

  def display_name
    text = to_s
    if birth_year
      death = death_year || "----"
      text += " (#{birth_year}-#{death})"
    end
    text += ", #{country.name}"
  end

end

class Country < ActiveRecord::Base
  active_admin_translates :name
end

I use active admin like this 我这样使用活动管理员

ActiveAdmin.register Painting do
end

The problem is than the display_name method need to call countries and translations tables. 问题在于, display_name方法需要调用国家和翻译表。 There is a lot of artists and it's very long to run. 有很多艺术家,而且运行时间很长。 I'm looking for a way to to increase the speed. 我正在寻找提高速度的方法。

Request seems like this : 请求看起来像这样:

SELECT "artists".* FROM "artists" WHERE "artists"."accepted" = 't' ORDER BY name
SELECT "countries".* FROM "countries" WHERE "countries"."id" = 50 ORDER BY name LIMIT 1

All artists are requested to do this input : 请所有艺术家输入以下内容: 选择图片

What can I do? 我能做什么?

Have you tried setting the scoped_collection on the controller? 您是否尝试过在控制器上设置scoped_collection?

I think it's something like this: 我认为是这样的:

ActiveAdmin.register Painting do
  controller do
    def scoped_collection
      Painting.joins({artist: {country: :translations}})
    end
  end
end

More info available here: http://www.activeadmin.info/docs/2-resource-customization.html 此处提供更多信息: http : //www.activeadmin.info/docs/2-resource-customization.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM