ActiveScaffold :: Wiki
Multi-layer belongs_to association

Let’s say you have a database with parent-child tables:

class OrderItem < ActiveRecord::Base
  belongs_to    :order
  delegate      :customer, :to => :order
end
class Order < ActiveRecord::Base
  belongs_to    :customer
end
class Customer < ActiveRecord::Base
end

You create an Active Scaffold for the order item that displays the order and the customer.

class SampleController < ApplicationController
  active_scaffold :order_item do |config|
    config.columns = ['customer', 'order', 'price']
  end
end

It doesn’t work. You cannot show the customer information because there’s no actual association to the customer record from the order_item record.

Here’s how to fix it (based on the nested include option).

class SampleController < ApplicationController
  active_scaffold :order_item do |config|
    config.columns = ['customer', 'order', 'price']
    config.columns[:customer].includes = [{'order' => 'customer'}]
  end
end

You can even have multiple nests. Assume that we add a table, Company, that customer belongs to.

class Customer < ActiveRecord::Base
  belongs_to    :company
end
class Company < ActiveRecord::Base
end

you can add another nested include like:

config.columns[:company].includes = [{'order' => {'customer' => 'company'}}]