Class Account < ActiveRecord::Base
delegate :firstname, :to => :user
# firstname => user.firstname
delegate :firstname, :to => :user, :prefix => true
# user_firstname => user.firstname
delegate :firstname, :to => :user, :allow_nil => true, :prefix => true
# user_firstname => user.try(:firstname)
delegate :name, :to => "user.try(:editor)", :prefix => :editor
# editor_name => user.editor.name
delegate :name, :to => "user.try(:editor)", :prefix => :editor, :allow_nil => true
# editor_name => user.editor.try(:name)
end
参考:https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/module/delegation.rb