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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:15:23+00:00 2026-05-14T06:15:23+00:00

For a Rails 3.0 Todo app, I have a Tasks model with a Status

  • 0

For a Rails 3.0 Todo app, I have a Tasks model with a Status field. What’s the best way to store the Status field data (field type) and still display a human-readable version in a view (HTML table)? Status can be:

0 = Normal
1 = Active
2 = Completed

Right now I have this:

Rails Schema Here:

create_table “tasks”, :force => true do |t|
t.integer “status”, :limit => 1, :default => 0, :null => false

Rails Model Here:

class Task < ActiveRecord::Base
  validates_inclusion_of :status, :in => 0..2,
    :message => "{{value}} must be 0, 1, or 2"

Rails View Here:

<h1>Listing tasks</h1>

<table>
  <tr>
    <th>Status</th>
    <th>Name</th>
    <th></th>
    <th></th>
    <th></th>
  </tr>

<% @tasks.each do |task| %>
  <tr>
    <td><%= task.status %></td>
    <td><%= task.name %></td>
    <td><%= link_to 'Show', task %></td>
    <td><%= link_to 'Edit', edit_task_path(task) %></td>
    <td><%= link_to 'Delete', task, :confirm => 'Are you sure?', :method => :delete %></td>
  </tr>
<% end %>
</table>

Requirements

  1. Store a Task’s status in the db such that the values are easily localizable, i.e. I’m not sure I want to store “normal”, “active”, “completed” as a string field.

  2. Solution must work with Rails 3.0.

Questions:

  1. Should I store the field as an integer (see above)? If so, how do I display the correct human readable status in an HTML table in my Rails view, e.g. show “Active” instead of “1” in the HTML table.

  2. Should I use an enum? If so, is this easy to localize later?

  3. Should I use straight strings, e.g. “Normal”, “Active”, “Completed”

  4. Can you provide a quick code sample of the view helper, controller or view code to make this work?

  • 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-14T06:15:24+00:00Added an answer on May 14, 2026 at 6:15 am

    1.It depends on how much you want to optimize queries on the DB.

    2.Not really, it is not supported ‘out of the box’ by AR. # As of Rails 4 enums are supported out of the box.

    3.IMHO you can use strings without a big performance penalty (just remember to add field to an index). I would do this because it’s easier to internationalize and to maintain. However, you can go with integers if you need extra performance.

    You may take a look on 2 SO threads here and here where this is debated.

    4.If you want to keep them as integer, here is how you can accomplish this:

    class Task << AR::Base
      NORMAL    = 1
      ACTIVE    = 2
      COMPLETED = 3
    
    
      STATUSES = {
        NORMAL    => 'normal',
        ACTIVE    => 'active',
        COMPLETED => 'completed'
      }
    
      validates_inclusion_of :status, :in => STATUSES.keys,
          :message => "{{value}} must be in #{STATUSES.values.join ','}"
    
      # just a helper method for the view
      def status_name
        STATUSES[status]
      end
    end
    

    and in view:

    <td><%= task.status_name %></td>
    

    If you want to use strings, it’s more simplified:

    STATUSES = ['normal', 'active', 'completed']
    validates_inclusion_of :status, :in => STATUSES,
              :message => "{{value}} must be in #{STATUSES.join ','}"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the best way to do per-user database connections in Rails ? I
I have an app where I want to link an instance of a model
I am writing my first rails app. It needs to aggregate some data from
I have a jruby/rails app using: jruby 1.4.0 Rails 2.3.5 ActiveMQ 5.3.0 Mule ESB
I'm building my first Rails project - its a ToDo app, which are supposed
I have recently been trying to upgrade my app form Rails 2.3.8 to newly-releases
I have a rails 2.3.5 app getting upgraded to Rails 3. I did every
I have a rails app that shows statistics based on an employee's daily production.
I have a rails CMS app that manages content for a production web server
I have a rails 2.3.5 app and if I try to do rake db:migrate,

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.