Last active
December 22, 2015 13:19
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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