Skip to content

Latest commit

 

History

History
11 lines (9 loc) · 384 Bytes

modify-attribute-on-set.md

File metadata and controls

11 lines (9 loc) · 384 Bytes

Modify Attribute on Set

In this example, we want to allow users to enter a twitter handle with or without an at-sign ('@'), just like Twitter, but save it without the at-sign. twitter_handle is already an attribute on the class.

class User < ActiveRecord::Base
  def twitter_handle=(handle)
    write_attribute(:twitter_handle, handle.to_s.gsub(/^@/, ''))
  end
end