在datagrid列中显示关联模型的属性

时间:2012-12-05 20:31:57

标签: datagrid model-associations

我有两个模型:UserDog。我希望能够在单个Datagrid报告中显示它们。使用column()方法时,我使用什么语法来引用用户模型的特定属性?现在我只是显示User对象,但我希望显示:name模型的:gender:ageUser属性的各个列。< / p>

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

1 个答案:

答案 0 :(得分:0)

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