在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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句