Skip to content

Describing Records: to_label

clst edited this page Sep 14, 2010 · 11 revisions

When ActiveScaffold needs to present a string description of a record, it searches through a common list of record properties looking for something that responds. The search set, in order, is: :to_label, :name, :label, :title, and finally :to_s. So if your schema already has one of those fields, it’ll be automatically used. But you can always define a to_label method to customize the string description.

Example:

class User < ActiveRecord::Base
  # ActiveScaffold will automatically use this name method to describe a 
  # record of this type
  def name
    "#{first_name} #{last_name}"
  end
end

class ForumPost < ActiveRecord::Base
  # Assuming that every Post has a title attribute, ActiveScaffold will 
  #automatically use that to describe a record of this type
end

class Car < ActiveRecord::Base
  # But you can always just define your own to_label method
  def to_label
    "#{year} #{model}"
  end
end

With my Rail 2.3.5 installation this did not work. I had to use:


def to_label
self[:my_name]
end

instead of

def to_label
“#{my_name}”
end

this would give me: ActionView::TemplateError (uninitialized constant MyClass::my_name) on line #19 of vendor/plugins/active_scaffold/frontends/default/views/_nested.html.erb
Clone this wiki locally