2014-01-28 68 views
0

关联模型Rails应用程序中,我有两个模式是这样的:如何包括active_admin

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 

所有艺术家被要求这样做输入: select image

我能做些什么?

+0

按照以下说明:https://github.com/josevalim/inherited_resources#overwriting-defaults – apneadiving

回答

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

+0

我纠正了我题。调用请求来执行输入。 – Dougui