在Rails中,我应该如何为“任务”应用程序(整数或枚举)实现“状态”字段? [英] In Rails, how should I implement a Status field for a Tasks app - integer or enum?

查看:123
本文介绍了在Rails中,我应该如何为“任务”应用程序(整数或枚举)实现“状态”字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于Rails 3.0 Todo应用程序,我有一个具有状态字段的Tasks模型。存储状态字段数据(字段类型)的最佳方法是什么,并仍然在视图(HTML表格)中显示可读的版本?状态可以是:

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 =正常

1 =活动

2 =已完成

0 = Normal
1 = Active
2 = Completed

现在我有这个:


create_tabletasks,:force => true do | t |

t.integerstatus,:limit => 1,,default => 0,:null => false

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



Rails Model Here:



Rails Model Here:

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



Rails查看此处: h2>

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. 将任务的状态存储在数据库中,使得值很容易本地化,即我不确定我是否要将normal,active,completed存储为字符串字段。

  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.

解决方案必须与Rails 3.0一起使用。

Solution must work with Rails 3.0.





Questions:


  1. 我应该将字段存储为整数(见上文)吗?如果是,如何在我的Rails视图中的HTML表格中显示正确的可读状态,例如在HTML表格中显示活动而不是1。

  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.

我应该使用枚举吗?如果是这样,以后容易本地化?

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

我应该使用直线字符串,例如正常,活动,已完成

Should I use straight strings, e.g. "Normal", "Active", "Completed"

可以提供视图帮助器,控制器或视图代码的快速代码示例,以使其正常工作吗?

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


推荐答案

1.这取决于您要对多少查询进行优化DB。

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

2.不是真的,它不支持开箱即用的AR。 #从Rails 4 枚举支持开箱即用

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可以使用字符串,而不会造成很大的性能损失(只需记住向索引添加字段)。我会这样做,因为它更容易国际化和维护。但是,如果您需要额外的表现,您可以使用整数。

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.

您可以查看2个SO线程这里这里这是辩论。

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

4.如果要保持它们为整数,这里是如何实现的:

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

并在视图中:

<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 ','}"

这篇关于在Rails中,我应该如何为“任务”应用程序(整数或枚举)实现“状态”字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆