Rails:从模型中的 datetime_select 中提取小时

Rails: Extract hour from datetime_select in model

我有一个使用 datetime_select 表单助手的表单,如下所示:

<div class="field">
    <%= f.label :start_date %><br>
    <%= f.datetime_select :start_date %>
</div>

我希望能够在我的模型中为构成该 DateTime 的各个字段解析该日期,但我似乎无法使用以下代码提取有效数据:

# This code is in the model, so there's no params hash
hour = self["start_date(4i)"].to_i

我正在使用 before_create 回调来调用包含相关代码的方法,它一直显示为“0”,而不考虑小时选项 select 中的实际设置看法。提取的小时将保存到整数类型的列中。

感谢您的任何见解。

你试过了吗hour = self.start_date.hour