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

  • SEARCH
  • Home
  • 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 7687479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:42:46+00:00 2026-05-31T19:42:46+00:00

Using Rails 3.1.3 and Ruby 1.9.3. I want to give the user a list

  • 0

Using Rails 3.1.3 and Ruby 1.9.3.

I want to give the user a list of possible date/time formats. The user’s selection will be stored in the Users table. Date/time values are then formatted using the I18n.localize function. I actually have 10 formats; here by way of example are the first two:

config/locales/datetime.en.yml

en:
  time:
    format_labels:
      mdyslash12: mm/dd/yyyy - hh:mm am (12-hour)
      mdyslash24: mm/dd/yyyy - hh:mm (24-hour)
    formats:
      mdyslash12: ! '%m/%d/%Y %I:%M%p'
      mdyslash24: ! '%m/%d/%Y %H:%M'

My question is where to store the list of possible date/time formats. I’ve identified three possibilities.

1. List options as a CONSTANT in model:

app/models/user.rb

DATETIME_FORMATS = %w[mdyslash12 mdyslash24]
validates :datetime_format, :presence => true,
          :inclusion => { :in => DATETIME_FORMATS }

2. Create an application constant and validate against that:

config/initializers/constants.rb

Rails.configuration.datetime_formats = "mdyslash12 mdyslash24"

app/models/user.rb

validates :datetime_format, :presence => true,
          :inclusion => { :in => Rails.application.config.datetime_formats.split(" ") }

3. Validate directly against the locale file:

app/models/user.rb

validates :datetime_format, :presence => true,
          :inclusion => { :in => (I18n.t 'time.format_labels').stringify_keys.keys }

This option uses a feature that is new to me: I18n.t 'time.format_labels' returns a hash of ALL keys and values from that branch of the locale file. The hash keys are symbols, so to get a string array, I call stringify_keys to convert the symbols to strings, then keys to give me only the keys (no values).

Option #3 is the DRYest in that I don’t have to list the possible values in two places. But it doesn’t feel quite right to depend on the locale file for the discreet list of possible date/time formats.

What would you recommend? One of these options? Something else?

  • 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-31T19:42:48+00:00Added an answer on May 31, 2026 at 7:42 pm

    I’d go with option 1 to start with, since it’s simple, clear, and fairly DRY. I might refactor to option 2 if I ended up needing that constant in another model.

    Option 3 has the potential to behave differently based on the locale, so I don’t like that. If you end up forgetting to specify your format labels in a new locale, your selection list might end up being empty (or if there’s a typo in one locale, it might take longer to notice, since the typo would be treated as valid for that locale). Regardless it’s probably a good idea to unittest this in all your supported locales.

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

Sidebar

Related Questions

I'm using Ruby on rails and I have videos that I don't want users
I am using Ruby on Rails. I want to create a filter field on
says if i develop a Ruby on Rails application using Rails 2.3.2, will that
I am using Ruby on Rails 3 and I would like to handle user
Im using Rails 3.0.3 and ruby 1.8.7 I want to render /materials/change_prize.html.erb in materials/change_prize.js.erb
So I installed Ruby On Rails using the Windows Installer. Now the startup guides
using Ruby on Rails 2.3.2, since I already created Scaffold for Story, so instead
I am using ruby on rails with a MySQL backend. I have a table
I'm using Ruby on Rails to make an application where businesses can create accounts
I am using Ruby on Rails and I need to store a search result

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.