2015-03-13 53 views
1

我在我的ActiveAdmin申請註冊到模型中的頁面如下:如何在ActiveAdmin 0.5.0中爲資源指定全局加載?

ActiveAdmin.register Report do 
    menu parent: 'Administration', priority: 2 

    scope ... 
    scope ... 

    filter ... 
    filter ... 

end 

「報告」模型/資源有許多其他模型關聯。什麼是最好的方式來指定也適用於所有範圍和過濾器結果的一些關聯的加載加載?

回答

1

閱讀更多關於資源定製,你可以覆蓋scoped_collection像

ActiveAdmin.register Report do 
    menu parent: 'Administration', priority: 2 

    controller do 
    def scoped_collection 
     Report.includes(:users, ....) 
    end 
    end 
end 

UPD,完全同意@TimoSchilling的評論。

如果你想覆蓋scoped_collection使用super然後追加方法,這樣,InheritedResource的end_of_association_chain不會被忽略。

所以最後的代碼是

ActiveAdmin.register Report do 
     menu parent: 'Administration', priority: 2 

     controller do 
     def scoped_collection 
      super.includes(:users, ....) 
      # or 
      super.eager_load(:users, ....) 
     end 
     end 
    end 

然而,在大多數情況下,所有這https://stackoverflow.com/a/29038410/246544#29038410答案將工作的偉大。

+1

這種方式的作品,但你應該使用'超'而不是'報告',否則你會失去像auth或過濾的東西。 – 2015-03-16 08:46:14

+0

@TemoSchilling,只有繼承的資源特性纔會被放棄,在''''''''''''''''''''''scoped_collection'''後面的'''find_collection''中附加了AA特性。 – Fivell 2015-03-16 17:07:41

2

最好的方法是使用內置功能:)

ActiveAdmin.register Report do 
    includes :users, :apples, :rhinos 

    ... 
end 

你可以在https://github.com/activeadmin/activeadmin/blob/master/docs/2-resource-customization.md

+0

使用'includes'時,我得到一個NoMethodError異常,它說'未定義的方法包含#' – moorara 2015-03-13 17:32:33

+0

您正在使用哪個版本的activeadmin? – unkmas 2015-03-13 17:34:12

+0

我認爲它是0.5.0 – moorara 2015-03-13 17:49:56