Ruby on rails Rails不正确地存储年份
我有一个Rails 4应用程序,正在尝试让管理员选择约会时间Ruby on rails Rails不正确地存储年份,ruby-on-rails,ruby,sqlite,Ruby On Rails,Ruby,Sqlite,我有一个Rails 4应用程序,正在尝试让管理员选择约会时间 ... <div class="field"> <%= f.label :start_time %><br> <%= f.datetime_select :start_time %> </div> <div class="field"> <%= f.label :end_time %><br> &
...
<div class="field">
<%= f.label :start_time %><br>
<%= f.datetime_select :start_time %>
</div>
<div class="field">
<%= f.label :end_time %><br>
<%= f.datetime_select :end_time %>
</div>
...
我已将默认时区更改为:
config.time_zone = "Pacific Time (US & Canada)"
问题
我通过表单创建,然后检查Rails控制台,如下所示:
Loading development environment (Rails 4.2.2)
>> appointment = Appointment.all.last
Appointment Load (0.2ms) SELECT "appointments".* FROM "appointments" ORDER BY "appointments"."id" DESC LIMIT 1
=> #<Appointment id: 8, user_id: 1, comments: "", start_time: "2000-01-01 12:50:00", end_time: "2000-01-01 12:50:00">
>> appointment.start_time.year
=> 2000
你能展示一下迁移过程吗?我认为您使用的是时间字段而不是日期时间检查打印约会参数中参数的值。我打赌你会得到start_date1i和类似的。好吧,所以时间不会存储年份信息,这就是datetime。好的,别忘了。时间确实存储年份。@乔丹:数据库中的时间应该是一天中的简单时间,即没有年、没有月、没有天,但Ruby没有日期类型,因此在Ruby中使用日期时间。Ruby时间实例将有一年,但数据库时间列不会。
Loading development environment (Rails 4.2.2)
>> appointment = Appointment.all.last
Appointment Load (0.2ms) SELECT "appointments".* FROM "appointments" ORDER BY "appointments"."id" DESC LIMIT 1
=> #<Appointment id: 8, user_id: 1, comments: "", start_time: "2000-01-01 12:50:00", end_time: "2000-01-01 12:50:00">
>> appointment.start_time.year
=> 2000
class ChangeTimeInAppointments < ActiveRecord::Migration
def change
remove_column :appointments, :time
add_column :appointments, :start_time, :time
add_column :appointments, :end_time, :time
end
end
create_table "appointments", force: :cascade do |t|
t.integer "user_id"
t.text "comments"
t.time "start_time"
t.time "end_time"
end