Frage

I have 3 classes:

app/model/order.rb

class Order < ActiveRecord::Base
    has_many :lines, dependent: :destroy
    accepts_nested_attributes_for :lines, allow_destroy: true
end

app/model/line.rb

class Line < ActiveRecord::Base
  belongs_to :order
  has_many :picklines, dependent: :destroy
end

app/model/pickline.rb

class Pickline < ActiveRecord::Base
  belongs_to :line
end

So, from the above, the structure as follows Order>Line>Pickline, i.e. Pickline will be 2nd level nested attrubte to Order.

I was able to read the column from Line (1st level nested) as below:

app/admin/order.rb

show do
   panel "Items" do
      table_for order.lines do
        column :description
      end
   end
end

Now, I'm having headache on how to retrieve, for example, column named "quantity" from the 2nd level nested attribute of Pickline...

I understand that I first have to iterate on each Line and then on each Pickline to retrieve any column from Pickline, but can't figure out how this can be done in ActiveAdmin.

War es hilfreich?

Lösung

2 options

1) you can use block inside column method

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) Try to use has_many :through

http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association

The has_many :through association is also useful for setting up "shortcuts" through nested has_many associations.

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

Than you can iterate over picklines

show do
   panel "Picklines" do
      table_for order.picklines do 
        column :description do |row|
            row.line.description
        end
        column :quantity  
      end
   end
end
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top