如何在 rails 模型中添加日期属性
How to add a date attribute in rails models
嗨,我是 RoR 的初学者,在生成特定模型时遇到了一些麻烦。
我想创建 2 个模型 - 列表和项目。列表 has_many 项目和项目 belongs_to 列表。
我希望 Item 模型有 3 个属性。 rails g model Item name:string desc:string date:????
1.What 要为 date:???
添加的数据类型
2.What 日期属性的格式是? (mm/dd/yy)?
3.And它应该有什么样的表单输入?
f.date_field :date
?
提前致谢!
您可以选择 date
or datetime
according to the documentation. 因此:
rails g model Item name:string desc:string date:datetime
或
rails g model Item name:string desc:string date:date
但是the best practice is to use DateTime as a general purpose representation of time.
尽管我可能会称它为比 date
更具描述性的名称。 (仅供参考 created_at
和 updated_at
列已经为您创建。)
该类型几乎与格式无关。您可以将其格式化为 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
最后就输入字段而言:看看 these Form helpers。
<%= date_field(:user, :born_on) %>
<%= datetime_field(:user, :meeting_time) %>
<%= datetime_local_field(:user, :graduation_day) %>
1。为 date:???
添加什么数据类型
在您的迁移中,您可以对列使用以下类型:
:string, :text, :integer, :float, :decimal, :datetime, :timestamp, :time, :date, :binary, :boolean
(提取自add_column
转换here)
在你的情况下,如果你不需要存储时间,你可以使用date:name_of_your_field
。
2。日期属性将采用什么格式? (mm/dd/yy)?
该属性将存储为 ActiveSupport::TimeWithZone
,您必须在显示时对其进行格式化。您可以使用 Time#strftime 来执行此操作。
your_attribute.strftime("%m/%d/%Y") #=> "11/19/2007"
3。它应该有什么样的表单输入?
是的,你可以完美使用:
f.date_field :date?
它将 return 一个 text_field
类型的“日期”。根据浏览器支持,日期选择器将显示在输入字段中。
希望对您有所帮助!编码愉快!
嗨,我是 RoR 的初学者,在生成特定模型时遇到了一些麻烦。
我想创建 2 个模型 - 列表和项目。列表 has_many 项目和项目 belongs_to 列表。
我希望 Item 模型有 3 个属性。 rails g model Item name:string desc:string date:????
1.What 要为 date:???
2.What 日期属性的格式是? (mm/dd/yy)?
3.And它应该有什么样的表单输入?
f.date_field :date
?
提前致谢!
您可以选择 date
or datetime
according to the documentation. 因此:
rails g model Item name:string desc:string date:datetime
或
rails g model Item name:string desc:string date:date
但是the best practice is to use DateTime as a general purpose representation of time.
尽管我可能会称它为比 date
更具描述性的名称。 (仅供参考 created_at
和 updated_at
列已经为您创建。)
该类型几乎与格式无关。您可以将其格式化为 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
最后就输入字段而言:看看 these Form helpers。
<%= date_field(:user, :born_on) %>
<%= datetime_field(:user, :meeting_time) %>
<%= datetime_local_field(:user, :graduation_day) %>
1。为 date:???
在您的迁移中,您可以对列使用以下类型:
:string, :text, :integer, :float, :decimal, :datetime, :timestamp, :time, :date, :binary, :boolean
(提取自add_column
转换here)
在你的情况下,如果你不需要存储时间,你可以使用date:name_of_your_field
。
2。日期属性将采用什么格式? (mm/dd/yy)?
该属性将存储为 ActiveSupport::TimeWithZone
,您必须在显示时对其进行格式化。您可以使用 Time#strftime 来执行此操作。
your_attribute.strftime("%m/%d/%Y") #=> "11/19/2007"
3。它应该有什么样的表单输入?
是的,你可以完美使用:
f.date_field :date?
它将 return 一个 text_field
类型的“日期”。根据浏览器支持,日期选择器将显示在输入字段中。
希望对您有所帮助!编码愉快!