将日期添加到活动记录中的日期属性
Adding a date to a date attribute in active record
我正在尝试为我的数据库中的每个患者添加出生日期,但在为每个患者的 dob
属性添加日期时遇到问题。当我添加前。 Patient.first(dob: "01/25/1990")
我在读取 no implicit conversion of Integer into Hash
时出错。关于我将如何做到这一点有什么想法吗?
create_table "patients", force: :cascade do |t|
t.string "first_name"
t.string "last_name"
t.integer "age"
t.date "dob"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
当我为我的数据库做种时,我的 dob
字段是 nil
我也试过Patient.first(dob: Date.parse('31-12-2010'))
,但仍然出现同样的错误。
你有两个问题:
first
不接受查询参数。
- 您的日期格式不明确。
first
finder method 看起来像:
first(limit = nil)
Find the first record (or first N
records if a parameter is supplied). If no order is defined it will order by primary key.
您想使用 find_by
作为您的 .where(...).first
快捷方式。
并且为了避免您的日期出现歧义,您应该在应用程序中使用 Date
对象或 ISO-8601 格式的字符串(即 YYYY-MM-DD),并保留所谓的 "human friendly" 格式最边缘。
您想说:
Patient.find_by(dob: '1990-01-25')
我正在尝试为我的数据库中的每个患者添加出生日期,但在为每个患者的 dob
属性添加日期时遇到问题。当我添加前。 Patient.first(dob: "01/25/1990")
我在读取 no implicit conversion of Integer into Hash
时出错。关于我将如何做到这一点有什么想法吗?
create_table "patients", force: :cascade do |t|
t.string "first_name"
t.string "last_name"
t.integer "age"
t.date "dob"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
当我为我的数据库做种时,我的 dob
字段是 nil
我也试过Patient.first(dob: Date.parse('31-12-2010'))
,但仍然出现同样的错误。
你有两个问题:
first
不接受查询参数。- 您的日期格式不明确。
first
finder method 看起来像:
first(limit = nil)
Find the first record (or firstN
records if a parameter is supplied). If no order is defined it will order by primary key.
您想使用 find_by
作为您的 .where(...).first
快捷方式。
并且为了避免您的日期出现歧义,您应该在应用程序中使用 Date
对象或 ISO-8601 格式的字符串(即 YYYY-MM-DD),并保留所谓的 "human friendly" 格式最边缘。
您想说:
Patient.find_by(dob: '1990-01-25')