public Method

Module.alias_attribute(new_name, old_name)

Allows you to make aliases for attributes, which includes getter, setter, and query methods.

Example:

class Content < ActiveRecord::Base
  # has a title attribute
end

class Email < Content
  alias_attribute :subject, :title
end

e = Email.find(1)
e.title    # => "Superstars"
e.subject  # => "Superstars"
e.subject? # => true
e.subject = "Megastars"
e.title    # => "Megastars"

Source Code

# File active_support/core_ext/module/aliasing.rb, line 63
def alias_attribute(new_name, old_name)
  module_eval "def \#{new_name}; self.\#{old_name}; end\ndef \#{new_name}?; self.\#{old_name}?; end\ndef \#{new_name}=(v); self.\#{old_name} = v; end\n", __FILE__, __LINE__+1
end
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.