Ruby-on-rails 如何在 Rails 模型中添加日期属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28293835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:03:57  来源:igfitidea点击:

How to add a date attribute in rails models

ruby-on-railsrubyactiverecord

提问by deventhusiast

Hi i'm a beginner in RoR and was having a little bit trouble in generating a particular model.

嗨,我是 RoR 的初学者,在生成特定模型时遇到了一些麻烦。

I want to create 2 models - List and Item. The List has_many Items and Item belongs_to List.

我想创建 2 个模型 - 列表和项目。列表 has_many Items 和 Item owns_to 列表。

I want the Item model to have 3 attributes. rails g model Item name:string desc:string date:????

我希望 Item 模型具有 3 个属性。 rails g model Item name:string desc:string date:????

1.What data type to add for date:???

1.添加什么数据类型 date:???

2.What format will the date attribute be in? (mm/dd/yy)?

2.日期属性是什么格式?(毫米/日/年)?

3.And what kind of form input should it have?

3.它应该有什么样的表单输入?

f.date_field :date?

f.date_field :date?

Thanks in advance!

提前致谢!

回答by Pablo Alonso

1. What data type to add for date:???

1. 为什么数据类型添加 date:???

In your migrations, you can use the following types for columns:

在您的迁移中,您可以对列使用以下类型:

:string, :text, :integer, :float, :decimal, :datetime, :timestamp, :time, :date, :binary, :boolean

(extracted from the add_columntransformation here)

(从这里add_column转换中提取)

In your case, if you don't need to store the time, you can use date:name_of_your_field.

在您的情况下,如果您不需要存储时间,则可以使用date:name_of_your_field.

2. What format will the date attribute be in? (mm/dd/yy)?

2. date 属性的格式是什么?(毫米/日/年)?

The attribute will be stored as a ActiveSupport::TimeWithZone, and you will have to format it when displaying it. You can use Time#strftimeto do so.

该属性将存储为ActiveSupport::TimeWithZone,您必须在显示时对其进行格式化。您可以使用Time#strftime来执行此操作。

your_attribute.strftime("%m/%d/%Y")   #=> "11/19/2007"

3. And what kind of form input should it have?

3.它应该有什么样的表单输入?

Yes, you can perfectly use:

是的,您可以完美地使用:

f.date_field :date?

It will return a text_fieldof type “date”. Depending on the browser support, a date picker will show up in the input field.

它将返回一个text_field“日期”类型。根据浏览器支持,日期选择器将显示在输入字段中。

I hope that helps! Happy coding!

我希望这有帮助!快乐编码!

回答by wpp

You can either have dateor datetimeaccording to the documentation.Therefore:

您可以拥有datedatetime根据文档。所以:

rails g model Item name:string desc:string date:datetime

or

或者

rails g model Item name:string desc:string date:date

Butthe best practice is to use DateTime as a general purpose representation of time.

最佳实践是使用 DateTime 作为时间的通用表示。

Although I would probably call it something more descriptive than date. (And just fyi created_atand updated_atcolumns are already created for you.)

尽管我可能会称它为比date. (并且已经为您创建了仅供参考created_atupdated_at列。)

The type is pretty much format agnostic. You can format it with strftime:

该类型几乎与格式无关。您可以使用以下格式对其进行格式化strftime

%Y%m%d           => 20071119                  Calendar date (basic)
%F               => 2007-11-19                Calendar date (extended)
%Y-%m            => 2007-11                   Calendar date, reduced accuracy, specific month
%Y               => 2007                      Calendar date, reduced accuracy, specific year
%C               => 20                        Calendar date, reduced accuracy, specific century
%Y%j             => 2007323                   Ordinal date (basic)
%Y-%j            => 2007-323                  Ordinal date (extended)
%GW%V%u          => 2007W471                  Week date (basic)
%G-W%V-%u        => 2007-W47-1                Week date (extended)
%GW%V            => 2007W47                   Week date, reduced accuracy, specific week (basic)
%G-W%V           => 2007-W47                  Week date, reduced accuracy, specific week (extended)
%H%M%S           => 083748                    Local time (basic)
%T               => 08:37:48                  Local time (extended)
%H%M             => 0837                      Local time, reduced accuracy, specific minute (basic)
%H:%M            => 08:37                     Local time, reduced accuracy, specific minute (extended)
%H               => 08                        Local time, reduced accuracy, specific hour
%H%M%S,%L        => 083748,000                Local time with decimal fraction, comma as decimal sign (basic)
%T,%L            => 08:37:48,000              Local time with decimal fraction, comma as decimal sign (extended)
%H%M%S.%L        => 083748.000                Local time with decimal fraction, full stop as decimal sign (basic)
%T.%L            => 08:37:48.000              Local time with decimal fraction, full stop as decimal sign (extended)
%H%M%S%z         => 083748-0600               Local time and the difference from UTC (basic)
%T%:z            => 08:37:48-06:00            Local time and the difference from UTC (extended)
%Y%m%dT%H%M%S%z  => 20071119T083748-0600      Date and time of day for calendar date (basic)
%FT%T%:z         => 2007-11-19T08:37:48-06:00 Date and time of day for calendar date (extended)
%Y%jT%H%M%S%z    => 2007323T083748-0600       Date and time of day for ordinal date (basic)
%Y-%jT%T%:z      => 2007-323T08:37:48-06:00   Date and time of day for ordinal date (extended)
%GW%V%uT%H%M%S%z => 2007W471T083748-0600      Date and time of day for week date (basic)
%G-W%V-%uT%T%:z  => 2007-W47-1T08:37:48-06:00 Date and time of day for week date (extended)
%Y%m%dT%H%M      => 20071119T0837             Calendar date and local time (basic)
%FT%R            => 2007-11-19T08:37          Calendar date and local time (extended)
%Y%jT%H%MZ       => 2007323T0837Z             Ordinal date and UTC of day (basic)
%Y-%jT%RZ        => 2007-323T08:37Z           Ordinal date and UTC of day (extended)
%GW%V%uT%H%M%z   => 2007W471T0837-0600        Week date and local time and difference from UTC (basic)
%G-W%V-%uT%R%:z  => 2007-W47-1T08:37-06:00    Week date and local time and difference from UTC (extended)

Thanks to @BWStearns for that quote

感谢@BWStearns 的报价

Finally as far as input field goes: Have a look at these Form helpers.

最后就输入字段而言:看看这些 Form helpers

<%= date_field(:user, :born_on) %>
<%= datetime_field(:user, :meeting_time) %>
<%= datetime_local_field(:user, :graduation_day) %>