Virtual Attribute - joining first_name and last_name into full_name with Rails

To join first_name and last_name into a virtual attribute full_name using Ruby on Rails check out Ryan’s Episode 16.

# This is your model
class User < ActiveRecord::Base
  def full_name
    [first_name, last_name].join(' ')
  end

  def full_name=(name)
    split = name.split(' ', 2)
    self.first_name = split.first
    self.last_name = split.last

  end
end

You can expand on that to do things like this:

  def full_name
    [first_name, middle_name, last_name].join(' ')
  end

  def full_name_backwards
    [last_name, [first_name, middle_name].join(' ')].join(', ')
  end

Comments