ActiveAdmin 索引中可用的实例变量
Instance variable available in ActiveAdmin index
我有一个 ActiveAdmin 资源,其收集需要一些特别复杂的查询以避免一堆 n+1。我尝试用所有正确的 .includes 或 .joins 修改 controller#scoped_collection,但它仍在执行 n+1。
从那以后,我改变了一种方法,即手动执行查询并将相关结果转储到哈希中。然后在我的索引视图中,我从哈希中获取数据,而不是从 scoped_collection.
不幸的是,我不知道如何使实例变量在索引范围内可用。我通过将它放入 params 散列(在索引范围内可用)来解决这个问题,但这最终会破坏过滤。
那么,有没有什么方法我没有看到从 scoped_collection 内部或索引内部可用的其他某种 before_filter-like 方法来限定变量的范围?
示例代码:
ActiveAdmin.register ComplexResource do
actions :index
controller do
def scoped_collection
@complex_collection_relation = ComplexResource.where(crazy: :stuff)
# a bunch more lines of code to do avoid n+1's and put info into supporting data hash
@supporting_data_hash = {}
complex_collection_relation.each{|r| supporting_data_hash[r.id] = stuff }
# my hacky workaround because @supporting_data_hash isn't available below
params[:supporting_data_hash] = @supporting_data_hash
return @complex_collection_relation
end
end
filter :my_filter_that_gets_broken, as: :string
index do
column :name
column :complex_attribute_1 do |r|
params[:supporting_data_hash][r.id][:complex_attribute_1]
end
# how can I do something like this without using the params workaround
# column :complex_attribute_1 do |r|
# @supporting_data_hash[r.id][:complex_attribute_1]
# end
end
end
@Jiemurat 对这个问题的回答就是解决方案。此问题与 How do I use instance variables, defined in the controller, in the view with ActiveAdmin?
重复
我知道这是一个老问题,但它是谷歌搜索时出现的第一个问题。另外,我不是 100% 确定它适用于重复的问题,所以我会在这里留下我的答案:
在为资源定义列时,实例变量可以作为帮助程序访问。例如:
ActiveAdmin.register Company do
controller do
before_action :load_data, only: :index
def load_data
@loaded_data = expensive_operation
end
end
index do
column 'Something' do |company|
loaded_data[company.id]
end
end
end
上找到了对它的引用
我有一个 ActiveAdmin 资源,其收集需要一些特别复杂的查询以避免一堆 n+1。我尝试用所有正确的 .includes 或 .joins 修改 controller#scoped_collection,但它仍在执行 n+1。
从那以后,我改变了一种方法,即手动执行查询并将相关结果转储到哈希中。然后在我的索引视图中,我从哈希中获取数据,而不是从 scoped_collection.
不幸的是,我不知道如何使实例变量在索引范围内可用。我通过将它放入 params 散列(在索引范围内可用)来解决这个问题,但这最终会破坏过滤。
那么,有没有什么方法我没有看到从 scoped_collection 内部或索引内部可用的其他某种 before_filter-like 方法来限定变量的范围?
示例代码:
ActiveAdmin.register ComplexResource do
actions :index
controller do
def scoped_collection
@complex_collection_relation = ComplexResource.where(crazy: :stuff)
# a bunch more lines of code to do avoid n+1's and put info into supporting data hash
@supporting_data_hash = {}
complex_collection_relation.each{|r| supporting_data_hash[r.id] = stuff }
# my hacky workaround because @supporting_data_hash isn't available below
params[:supporting_data_hash] = @supporting_data_hash
return @complex_collection_relation
end
end
filter :my_filter_that_gets_broken, as: :string
index do
column :name
column :complex_attribute_1 do |r|
params[:supporting_data_hash][r.id][:complex_attribute_1]
end
# how can I do something like this without using the params workaround
# column :complex_attribute_1 do |r|
# @supporting_data_hash[r.id][:complex_attribute_1]
# end
end
end
@Jiemurat 对这个问题的回答就是解决方案。此问题与 How do I use instance variables, defined in the controller, in the view with ActiveAdmin?
重复我知道这是一个老问题,但它是谷歌搜索时出现的第一个问题。另外,我不是 100% 确定它适用于重复的问题,所以我会在这里留下我的答案:
在为资源定义列时,实例变量可以作为帮助程序访问。例如:
ActiveAdmin.register Company do
controller do
before_action :load_data, only: :index
def load_data
@loaded_data = expensive_operation
end
end
index do
column 'Something' do |company|
loaded_data[company.id]
end
end
end
上找到了对它的引用