[英]How to includes associated models in active_admin
在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
我這樣使用活動管理員
ActiveAdmin.register Painting do
end
問題在於, display_name
方法需要調用國家和翻譯表。 有很多藝術家,而且運行時間很長。 我正在尋找提高速度的方法。
請求看起來像這樣:
SELECT "artists".* FROM "artists" WHERE "artists"."accepted" = 't' ORDER BY name
SELECT "countries".* FROM "countries" WHERE "countries"."id" = 50 ORDER BY name LIMIT 1
請所有藝術家輸入以下內容:
我能做什么?
您是否嘗試過在控制器上設置scoped_collection?
我認為是這樣的:
ActiveAdmin.register Painting do
controller do
def scoped_collection
Painting.joins({artist: {country: :translations}})
end
end
end
此處提供更多信息: http : //www.activeadmin.info/docs/2-resource-customization.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.