[英]Rails rake task with update_attribute results in Mysql2::Error: Unknown column 'NaN' in 'field list'
I'm experiencing an error I can't seem to figure out when trying to update a field in my database. 我在尝试更新数据库中的字段时遇到错误,但似乎无法弄清。 I'm using rails with update_attribute via rake task. 我正在通过rake任务在update_attribute中使用rails。 What is strange is that the error relates to a column that is not used in the task or any of the methods. 奇怪的是,该错误与该任务或任何方法中未使用的列有关。
I've tried using update_attributes
as well as position.save(validate: false)
but they all result in the same error. 我试过使用update_attributes
以及position.save(validate: false)
但它们都导致相同的错误。 Any help is greatly appreciated. 任何帮助是极大的赞赏。
The error: 错误:
Mysql2::Error: Unknown column 'NaN' in 'field list': UPDATE `positions` SET `is_open` = 0, `cost_per_share` = NaN WHERE `positions`.`id` = 617
The line the error is traced to: 错误跟踪到的行:
position.update_attribute(:is_open, open)
The task source code: 任务源代码:
namespace :tom do
desc "Update positions.is_open field"
task check_if_open: :environment do
include TomDate
positions = Position.all
positions.each do |position|
if position.orange_highlight == 1
open = 0
elsif position.buy_misc == 'EXP' || position.buy_misc == 'ASSIGN' || position.sell_misc == 'EXP' || position.sell_misc == 'ASSIGN'
if position.profit == 0 && position.loss == 0
open = 1
elsif position.profit > 0
open = 0
elsif position.loss < 0
open = 0
else
open = 1
end
elsif tom_is_date_valid(position.buy_date) || tom_is_date_valid(position.sell_date)
if position.profit == 0 && position.loss == 0
open = 1
elsif position.profit > 0
open = 0
elsif position.loss < 0
open = 0
else
open = 1
end
else
open = 1
end
position.update_attribute(:is_open, open)
end
end
end
I managed to run the update successfully after changing the update_attribute
line to: 在将update_attribute
行更改为以下内容后,我设法成功运行了更新:
sql = "UPDATE positions SET is_open = #{open} WHERE id = #{position.id}"
ActiveRecord::Base.connection.execute(sql)
I'm not sure why the update_attribute fails. 我不确定为什么update_attribute失败。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.