Search code examples
datagridmodel-associations

Displaying attributes of associated models in a datagrid column


I have two models: User and Dog. I want to be able to show them both in a single Datagrid report. What syntax do I use to refer to a specific attribute of the user model when using the column() method? Right now I am just displaying the User object but I would like to display various columns with :name, :gender and :age attributes of the User model.

class User < ActiveRecord::Base
  attr_accessible :email, :age, :gender, :name
  has_many :dogs

class Dog < ActiveRecord::Base
  attr_accessible :name, :age
  belongs_to :user

class DogReport
    include Datagrid

  #
  # Scope
  #

  scope do
    Dog.includes(:user)
  end

  #
  # Filters
  #

  filter(:dog_id, :integer)

  #
  # Columns
  #

  column(:id)
  column(:name)
  column(:age)
  column(:user)
end

Solution

  • column(:user, :header => "user.name") do
      self.user.name
    end