Ruby datetime属性值未保存在SQLite DB中

dougiebuckets

我正在尝试将具有'sale_price'(浮动)和'order_date'(日期时间)属性的Order对象保存到SQLite数据库。日志中的所有内容看起来都好像为每个Order对象正确保存了两种属性类型。但是,当我检查数据库时,所有Order对象的'order_date'属性均为nil。这是代码:

response["orders"].each do |n|

        balance = n["balance"]

        sale_price = balance.to_f

        order_date = n["created_date"]

        order = Order.new(sale_price: sale_price, order_date: order_date)

        if Order.where(order_date: order_date).exists?

            puts "That order already exists in the DB"

        else

            if (order.sale_price || order.order_date) != nil 

                order.save

            else
                p "The sale price is nil"
            end

        end     

    end

这是日志中的示例事务外观:

    Order Exists (0.9ms)  SELECT 1 AS one FROM "orders" WHERE "orders"."order_date" = '2013-12-06T15:53:36.424Z' LIMIT 1
   (0.1ms)  begin transaction
  SQL (0.5ms)  INSERT INTO "orders" ("created_at", "order_date", "sale_price", "updated_at") VALUES (?, ?, ?, ?)  [["created_at", Tue, 27 May 2014 22:59:02 UTC +00:00], ["order_date", Fri, 06 Dec 2013 15:53:36 UTC +00:00], ["sale_price", 20548.0], ["updated_at", Tue, 27 May 2014 22:59:02 UTC +00:00]]
   (3.1ms)  commit transaction

有什么想法吗?

约翰·韦伯斯特

(来自@dougiebuckets答案中提到的John W)我认为最有可能发生的事情(以及为什么删除sqlite db文件起作用的原因)是您正在查看创建的第一个记录。由于您在以后的迁移中将order_date字段添加到Order中,因此这些第一条记录的order_date值为nil,因为创建该字段时该字段不存在。

我也被骗了,因为我可以访问@dougiebuckets源代码。:)但这是我的第一个SO答案,所以请给我一些时间。:D

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章