Rails: Non-DB field in Model with validation

Challenge: Add a field to the model representing system date, time & timezone that is not stored in the database and have validation on it.

Solution:

  • Add attr_accessor & attr_accessible (for bulk update), add getter (sys_date_time) & setter (sys_date_time=) methods.
  • Return system date time in UTC from getter and show in user (browser) timezone on client side (using moment-timezone.js which is moment.js extension select┬áthe download with timezone data)
  • When the setting come to be saved using update_attributes in controller it eventually calls model setter (sys_date_time=) where you convert it back to utc given the timezone info (ActiveSupport::Timezone[zone] is your friend with ActiveSupport::TimeZone[zone].parse then in_time_zone(‘UTC’))
  • Validation is tricky as this is not a db backed and getter returns the calculated value which is always valid. You need to validate input first thing in setter and set a new member variable with errors found. Then add a validate do … end block where you iterate through the new member variable with errors and do errors.add(attr, message).

Will add code snippets when time permits.