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. 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. 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:
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.