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 6687801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:19:52+00:00 2026-05-26T05:19:52+00:00

I have been looking everywhere but can’t seem to find a good solution for

  • 0

I have been looking everywhere but can’t seem to find a good solution for this.

My form has a date (textfield with datepicker) and a time (textfield with timepicker), which I want to map to an model field called due_at.

So far I been handling it in my controller with separate parameters to join it up to a datetime then set the model field manually, but it’s messy and really think this logic should be kept in model/view.

I would like to be able to handle the two form fields to an attribute in the model, then split it back out for errors, edit action etc. Basically a custom way of performing what the standard datetime_select does, but putting my own touch to it.

Is there something that I can put in my model like ?

def due_at=(date, time)
...
end

I been looking a number of places, but can’t find out how you would do this. People say to use javascript to populate a hidden field, but just don’t seem like the cleanest solution for a pretty simple problem.

Any advice/help would be much appreciated.

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-26T05:19:52+00:00Added an answer on May 26, 2026 at 5:19 am

    First: please rename your field because created_at may cause conflicts with ActiveRecord.

    I did exactly this for a field with the format M/D/YYYY H:M (Hours/Minutes in 24hrs format)

    In your model:

    attr_accessor :due_date, :due_time
    
    before_validation :make_due_at
    
    
    def make_due_at
      if @due_date.present? && @due_time.present?
        self.due_at = DateTime.new(@due_date.year, @due_date.month, @due_date.day, @due_time.hour, @due_time.min)
      end
    end
    
    
    def due_date
      return @due_date if @due_date.present?
      return @due_at if @due_at.present?
      return Date.today
    end
    
    def due_time
      return @due_time if @due_time.present?
      return @due_at if @due_at.present?
      return Time.now
    end 
    
    
    def due_date=(new_date)
      @due_date = self.string_to_datetime(new_date, I18n.t('date.formats.default'))
    end
    
    
    def due_time=(new_time)
      @due_time = self.string_to_datetime(new_time, I18n.t('time.formats.time'))
    end
    
    protected
    
    def string_to_datetime(value, format)
      return value unless value.is_a?(String)
    
      begin
        DateTime.strptime(value, format)
      rescue ArgumentError
        nil
      end
    end
    

    now in the view:

    <%= text_field_tag :due_time, I18n.l(@mymodel.due_time, :format => :time) %>
    <%= text_field_tag :due_date, I18n.l(@mymodel.due_date, :format => :default) %>
    

    now in the config/locales/en.yml (if english)

      date:
        formats:
          default: "%m/%d/%Y"
      time:
        formats:
          time: "%H:%M"
    

    You may change the date format of course.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ive been looking everywhere for a possible solution to this but can't seem to
I have been looking everywhere but I can't find a site that shows how
I have been looking over the web for this but I can't seem to
I've been looking everywhere but can't seem to find any examples/tutorials for my situation
I have been looking everywhere and can't find this.. I got a string and
I have been looking around a lot but i simply can't find a nice
I have been looking everywhere but I cannot seem to google it right so
Been searching everywhere and can't find a way to do this. I have a
I've been looking everywhere and I can't seem to find a reliable mouseenter event.
I have been looking everywhere for an answer to this question - perhaps I'm

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.