Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 649003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:53:43+00:00 2026-05-13T21:53:43+00:00

I have a model Coupon with an attribute expired_at , of class DateTime and

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T21:53:43+00:00Added an answer on May 13, 2026 at 9:53 pm

    You can change the default timezone for your rails application by changing config.time_zone in your environment.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 save logic. You just need to change your retrieval
    logic. Use the in_time_zone method of Time class.

    c =  Coupon.last
    p c.expired_at.in_time_zone(c.expired_at_timezone)
    # => Tue, 09 Mar 2010 02:06:00 MST -07:00
    

    Otherwise you can override the expired_at method of your Coupon model.

    def expired_at
      # access the current value of expired_at from attributes hash
      attributes["expired_at"].in_time_zone(self.expired_at_timezone)
    end
    

    Now you can do the following:

    p Coupon.last.expired_at
    # => Tue, 09 Mar 2010 02:06:00 MST -07:00
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have model: # encoding: utf-8 class Tag include Mongoid::Document field :name, type: String
I have model Post with boolean field default. Post belongs to User. I want
I have model: class Task include Mongoid::Document field :name, type: String field :category, type:
I have a coupon model and i am trying to calculate how much you
I have model Foo which has field bar. The bar field should be unique,
I have model Article it has field title with some text that may contain
I have model public class UploadOptionModel { public UploadOptionModel() { OutputFormat = outputFormatList.Select(i =>
I have model: [XmlRoot(ElementName = event, IsNullable=true)] public class Event { public int id
Lets say I have model inheritance set up in the way defined below. class
I have django models with a manytomany connection with a through class: class userProfile(models.Model):

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.