Skip to content

Describing Records: to_label

cntrytwist 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
  1. ActiveScaffold will automatically use this name method to describe a
  2. record of this type
    def name
    “#{first_name} #{last_name}”
    end
    end

class ForumPost < ActiveRecord::Base

  1. 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

  1. But you can always just define your own to_label method
    def to_label
    “#{year} #{model}”
    end
    end
Clone this wiki locally