Search code examples
ruby-on-railsactivescaffold

Active_scaffold add to list associated table column


class Agency < ActiveRecord::Base
  has_many :events

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  attr_accessible :name, :email, :phone, :address, :city, :state, :zip,
                  :notes, :is_admin, :password, :password_confirmation, :remember_me
end

class Event < ActiveRecord::Base
  belongs_to :agency
  has_many :consumers
 end

class Consumer < ActiveRecord::Base
  belongs_to :event
end

In consumers_controller I am trying to include some field from agency

 active_scaffold :consumer do |conf|
    list.columns = [
      :agency, :event
    ]
 end

There are such associations Agency -> Event -> Consumer. And there is no association between agency and consumer, only through event.

but it causes an error.

How Can I include to list a any field form agency table?


Solution

  • Solution was a quite simple but most likely inefficient.

    I have added a method to Consumer model:

    class Consumer < ActiveRecord::Base
      ...
    
      def agency_name
        self.event.agency[:name]
      end
    end
    

    Then I have added a virtual column to list:

    list.columns = [
      :agency_name, :event, ... ]
    

    That's all.