Easily specify which method to use to display an object. It was created to work in Rails models, though it works with any Ruby class.
gem install display_name
class Person < ActiveRecord::Base display_name :full_name end
This will generate a method “display_name” for the record, which will return the full name. It will also override the “to_s” method, so in the view you could use:
<%= person %>
That would automatically display the full name of the person. It also helps populating selects for lists of records.
Note that the display_name can be any method, and not necessary a database column.
Copyright © 2010 Javier Martín. See LICENSE for details.