您将如何从同级 table 添加 Active Admin 索引列?

How would you add Active Admin index columns from a sibling table?

在我的 Customers_Rewards 活动管理员中,我目前显示来自 Customer_Rewards 和客户数据 table 的列,但我也想显示列(例如 store_id)来自订单数据 table。

ActiveAdmin.register CustomerReward do
    menu :parent=>"Customers", :priority=>10, :label=>"Activated Rewards"

  index do
    selectable_column
    column("Customer ID"){|u| u.customer.id }
    column("Store ID"){|u| u.order.store_id }  #DOES NOT WORK
    column("First name"){|u| u.customer.first_name }
    column("Last name"){|u| u.customer.last_name }
    column("Email"){|u| u.customer.email }
    column :reward
    column :points_redeemed
    column :expires_at
    column :redeemed_at
    bool_column :is_active
    actions
  end
 _____________________________________________________
class CustomerReward < ActiveRecord::Base
  belongs_to :customer
end

class Customer < ActiveRecord::Base
  has_many :addresses, :order => 'is_active desc, is_default desc'
  has_many :customer_rewards
  has_many :orders
end

class Order < ActiveRecord::Base
  belongs_to :customer
end

I get an error message of: 
 undefined method `order' for #<Customer:0x007fb1dc2e3160>

u.order.store_id 表示您告诉 Rails 从 orders table 和 customer_rewards table 之间的关联中获取 order,但是没有这样的关系。

不过你可以做的是打电话给

u.customer.orders

这将 return 与 CustomerReward 对象 (u) 相关的客户的所有订单。

我不知道你会根据什么获取具体的 store_id,因为

u.customer.orders

会return一个集合,因此会有store_id个集合——你可以调用

查看
u.customer.orders.pluck(:store_id) # same as map(&:store_id)