I have a model Coupon with an attribute expired_at, of class DateTime and before I save the record, I want to change the zone part of the field according to the user’s choice. Say,
c = Coupon.new
c.expired_at = DateTime.now
c.expired_at_timezone = "Arizona"
c.save!
And in coupon.rb:
class Coupon < ActiveRecord::Base
def before_save
# change the zone part here, leave the date and time part alone
end
end
What I’m saying is if the admin wants the coupon expired at 2014-07-01 10:00 am, Arizona time, the expired_at stored in the DB should be something like this:
Tue, 01 Jul 2014 10:00:00 MST -07:00
Is there any way I can modify the zone part only and leave the date and time part alone?
Thanks
You can change the default timezone for your rails application by changing
config.time_zonein yourenvironment.rb. Usually by default it is set to UTC.In your case each coupon has its own timezone. So you have to use a different approach.
You don’t have to change your
savelogic. You just need to change your retrievallogic. Use the in_time_zone method of Time class.
Otherwise you can override the
expired_atmethod of your Coupon model.Now you can do the following: