如何使用两个参数更新 rails 中的数据?
How to update data in rails with two parameters?
我想更新数据,但我没有使用按id查找;相反,我搜索 id 和 date 来获取数据。
这是我的代码:
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
程序不会执行更新。一查,立马returns这个错误。
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)
将first
添加到查询中,
@timerecords = Timerecords.where(empid: empid, date: date.to_date).first
我想更新数据,但我没有使用按id查找;相反,我搜索 id 和 date 来获取数据。 这是我的代码:
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
程序不会执行更新。一查,立马returns这个错误。
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)
将first
添加到查询中,
@timerecords = Timerecords.where(empid: empid, date: date.to_date).first