我有3个课程:
应用/模型/ order.rb
class Order < ActiveRecord::Base
has_many :lines, dependent: :destroy
accepts_nested_attributes_for :lines, allow_destroy: true
end
应用/模型/ line.rb
class Line < ActiveRecord::Base
belongs_to :order
has_many :picklines, dependent: :destroy
end
应用/模型/ pickline.rb
class Pickline < ActiveRecord::Base
belongs_to :line
end
因此,从上面开始,结构如下Order>Line>Pickline
,即Pickline
将是第二级嵌套attrubte到Order
。
我能够从Line
(第一级嵌套)中读取列,如下所示:
应用/管理/ order.rb
show do
panel "Items" do
table_for order.lines do
column :description
end
end
end
现在,我对如何从Pickline
的第二级嵌套属性检索名为“quantity”的列感到头疼......
我知道我首先必须遍历每个Line
,然后在每个Pickline
上迭代以从Pickline检索任何列,但无法弄清楚如何在ActiveAdmin中完成此操作。
答案 0 :(得分:2)
2个选项
1)你可以使用block inside column方法
show do
panel "Items" do
table_for order.lines do
column :description
column :quantity do |line|
line.picklines.count #<-- you can access picklines for each line here
end
end
end
end
2)尝试使用has_many :through
http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association
has_many :through
关联对于通过嵌套的has_many关联设置“快捷方式”也很有用。
class Order < ActiveRecord::Base
has_many :lines, dependent: :destroy
has_many :picklines, through: :lines #<---- this line
accepts_nested_attributes_for :lines, allow_destroy: true
end
比你可以迭代挑选线
show do
panel "Picklines" do
table_for order.picklines do
column :description do |row|
row.line.description
end
column :quantity
end
end
end