Skip to content

Instantly share code, notes, and snippets.

@jameshibbard
Last active December 22, 2015 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jameshibbard/6478679 to your computer and use it in GitHub Desktop.
Save jameshibbard/6478679 to your computer and use it in GitHub Desktop.
Demonstrates the use of virtual attributes to create form fields that do not map directly to the database
class Birthday < ActiveRecord::Base
attr_accessible :date, :name, :day, :month, :year
before_validation :make_date
def day
(date.nil?)? @day : date.split("-")[0]
end
def month
(date.nil?)? @month : date.split("-")[1]
end
def year
(date.nil?)? @year : date.split("-")[2]
end
def day=(val)
@day = val
end
def month=(val)
@month = val
end
def year=(val)
@year = val
end
def make_date
birthday = [@day, @month, @year].join("-")
self.date = (birthday == "--")? "" : birthday
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment