我想更新数据但我没有使用find by id;相反,我搜索id和日期来获取数据。 这是我的代码:
def update_time
# get the parameters
puts 'update time'
date = params[:date]
status=params[:status]
empid=params[:empid]
# find the timerecords
@timerecords = Timerecords.where(empid: empid, date: date.to_date)
puts 'after find'
# update the timerecords
@timerecords.update_attributes(timeout: Time.zone.now, status: status, empid: empid)
puts 'after update'
redirect_to @timerecords
end
该程序不会执行更新。搜索后,它会立即返回此错误。
Completed 500 Internal Server Error in 7ms
NoMethodError (undefined method `update_attributes' for # <Timerecords::ActiveRecord_Relation:0x82b6000>):
app/controllers/timerecords_controller.rb:24:in `update_time'
这是第24行的代码
@timerecords.update_attributes(timeout: Time.zone.now, status: status, empid: empid)
答案 0 :(得分:1)
将first
添加到查询
@timerecords = Timerecords.where(empid: empid, date: date.to_date).first