Ruby:如何存储和显示一周中的某一天?

时间:2022-10-21 10:48:15

I'm relatively new to Ruby. I need to keep track of a day of the week on my activity model. The code that I have so far doesn't seem like the most elegant solution. I am wondering if there is a better way.

我对Ruby比较陌生。我需要跟踪我的活动模型中的一周中的某一天。到目前为止,我所拥有的代码似乎不是最优雅的解决方案。我想知道是否有更好的方法。

So far, this is what I have in my form:

到目前为止,这就是我的形式:

<%= form_for(@activity) do |f| %>
  <div class="field">
    <%= f.label :day %><br />
    <%= f.select :day, [['Sunday', 0], ['Monday', 1], ['Tuesday', 2], ['Wednesday', 3], ['Thursday', 4], ['Friday', 5], ['Saturday', 6]] %>
  </div>
<% end %>

And when I need to display the day of the week:

当我需要显示星期几时:

<% days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'] %>
<% @activities.each do |activity| %>
  <%= days[activity.day.to_i] %>
<% end %>

How can I do this better? Feel free to mention any library/gem that I may not be aware of.

我怎么能做得更好?随意提及我可能不知道的任何图书馆/宝石。

Solution:

Ruby provides the Date::DAYNAMES constant which is an array of days of the week. We can reuse that in our code:

Ruby提供Date :: DAYNAMES常量,它是一周中的几天。我们可以在代码中重用它:

<%= form_for(@activity) do |f| %>
  <div class="field">
    <%= f.label :day %><br />
    <%= f.select :day, Date::DAYNAMES.zip((0..6).to_a) %>
  </div>
<% end %>

<% @activities.each do |activity| %>
  <%= Date::DAYNAMES[activity.day.to_i] %>
<% end %>

4 个解决方案

#1


15  

Ruby's Date class already has a constant defined that holds the name of the days in an array: Date::DAYNAMES.

Ruby的Date类已经定义了一个常量,它保​​存数组中的日期名称:Date :: DAYNAMES。

For example, you could do something like:

例如,您可以执行以下操作:

days = []
Date::DAYNAMES.each_with_index { |x, i| days << [x, i] }

I don't know if I'd necessarily say that this is better, but you can definitely leverage what is already available to you.

我不知道我是否一定会说这更好,但你绝对可以利用已有的东西。

#2


3  

The easiest way (RoR) is using the built-in constant:

最简单的方法(RoR)使用内置常量:

Date::DAYS_INTO_WEEK
# => 
{:monday=>0,
 :tuesday=>1,
 :wednesday=>2,
 :thursday=>3,
 :friday=>4,
 :saturday=>5,
 :sunday=>6}

#3


0  

To keep it simplest, I would :

为了保持最简单,我会:

  • save the plain text name of the day instead of number in @activity.day
  • 保存当天的纯文本名称而不是@ activity.day中的数字
  • add a constant DAYS, it can live on your Activity model but may move to somewhere else in the future
  • 添加一个恒定的DAYS,它可以存在于您的Activity模型中,但可能会在未来移动到其他地方

Then it will looks like :

然后它看起来像:

Model

模型

DAYS = ['monday','tuesday',...]

validates_inclusion_of :day, :in => DAYS

View : form

查看:表格

<%= f.collection_select :day, Activity::DAYS, :to_s, :humanize %>

View : display

查看:显示

<%= activity.day.humanize %>

Keep it simple and you will extend it later if needed :)

保持简单,如果需要你会延长它:)

#4


0  

In the latest versions you can define an enum with that constant and then use it for different things:

在最新版本中,您可以使用该常量定义枚举,然后将其用于不同的事物:

In migration, day_of_week is integer

在迁移中,day_of_week是整数

In model:

在模型中:

enum :day_of_week => Date::DAYS_INTO_WEEK

枚举:day_of_week =>日期:: DAYS_INTO_WEEK

And then you can use searches in model

然后你可以在模型中使用搜索

Model.day_of_weeks.keys[0..4] will return ['monday', 'tuesday', .... 'friday']

Model.day_of_weeks.keys [0..4]将返回['monday','tuesday',....'friday']

Model.monday is equivalent to Model.where("day_of_week = ?", 'monday')

Model.monday相当于Model.where(“day_of_week =?”,“monday”)

http://api.rubyonrails.org/classes/ActiveRecord/Enum.html

http://api.rubyonrails.org/classes/ActiveRecord/Enum.html

#1


15  

Ruby's Date class already has a constant defined that holds the name of the days in an array: Date::DAYNAMES.

Ruby的Date类已经定义了一个常量,它保​​存数组中的日期名称:Date :: DAYNAMES。

For example, you could do something like:

例如,您可以执行以下操作:

days = []
Date::DAYNAMES.each_with_index { |x, i| days << [x, i] }

I don't know if I'd necessarily say that this is better, but you can definitely leverage what is already available to you.

我不知道我是否一定会说这更好,但你绝对可以利用已有的东西。

#2


3  

The easiest way (RoR) is using the built-in constant:

最简单的方法(RoR)使用内置常量:

Date::DAYS_INTO_WEEK
# => 
{:monday=>0,
 :tuesday=>1,
 :wednesday=>2,
 :thursday=>3,
 :friday=>4,
 :saturday=>5,
 :sunday=>6}

#3


0  

To keep it simplest, I would :

为了保持最简单,我会:

  • save the plain text name of the day instead of number in @activity.day
  • 保存当天的纯文本名称而不是@ activity.day中的数字
  • add a constant DAYS, it can live on your Activity model but may move to somewhere else in the future
  • 添加一个恒定的DAYS,它可以存在于您的Activity模型中,但可能会在未来移动到其他地方

Then it will looks like :

然后它看起来像:

Model

模型

DAYS = ['monday','tuesday',...]

validates_inclusion_of :day, :in => DAYS

View : form

查看:表格

<%= f.collection_select :day, Activity::DAYS, :to_s, :humanize %>

View : display

查看:显示

<%= activity.day.humanize %>

Keep it simple and you will extend it later if needed :)

保持简单,如果需要你会延长它:)

#4


0  

In the latest versions you can define an enum with that constant and then use it for different things:

在最新版本中,您可以使用该常量定义枚举,然后将其用于不同的事物:

In migration, day_of_week is integer

在迁移中,day_of_week是整数

In model:

在模型中:

enum :day_of_week => Date::DAYS_INTO_WEEK

枚举:day_of_week =>日期:: DAYS_INTO_WEEK

And then you can use searches in model

然后你可以在模型中使用搜索

Model.day_of_weeks.keys[0..4] will return ['monday', 'tuesday', .... 'friday']

Model.day_of_weeks.keys [0..4]将返回['monday','tuesday',....'friday']

Model.monday is equivalent to Model.where("day_of_week = ?", 'monday')

Model.monday相当于Model.where(“day_of_week =?”,“monday”)

http://api.rubyonrails.org/classes/ActiveRecord/Enum.html

http://api.rubyonrails.org/classes/ActiveRecord/Enum.html