1

I'm trying to save Order objects with 'sale_price' (float) and 'order_date' (datetime) attributes to a SQLite DB. Everything in the logs makes it look as though both attribute types are saved properly for each Order object. However, when I check the DB, all of the Order objects 'order_date' attributes are nil. Here's the code:

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

Here's what a sample transaction looks like in the logs:

    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

Any thoughts?

10
  • Can you make a puts of order.sale_price and order.order_date before saving and show what's logging?
    – FabKremer
    May 28, 2014 at 1:15
  • Here you go: 20828.0 2014-05-07 16:28:34 UTC May 28, 2014 at 2:17
  • Are all the other attributes what you expect them to be? Are you absolutely sure you're looking at the right db file? (check your database.yml to be sure)
    – PinnyM
    May 28, 2014 at 2:28
  • Yes. Everything else is fine except the order_date attribute value is nil for all entries. Here's a look from console: [#<Order id: 1, created_at: "2014-05-27 21:54:15", updated_at: "2014-05-27 21:54:15", sale_price: 20828.0, order_date: nil> May 28, 2014 at 2:37
  • 1
    Hmmm... And what's your order_date column type? Perhaps you're having a type error, so your data is not being assigned correctly. Check that you've set timestamp type in order_date.
    – FabKremer
    May 28, 2014 at 4:21

2 Answers 2

1

(From the John W mentioned in @dougiebuckets answer) I think what was most likely happening (and why deleting your sqlite db file worked) is that you were looking at the first records created. Since you added the order_date field to Order in a later migration, those first records would have a nil value for order_date since that field didn't exist when they were created.

I also cheated because I had access to @dougiebuckets source code. :) But this is my first SO answer, so cut me some slack. :D

1

Deleting 'development.sqlite3' and re-running my migrations fixed it (Thanks John W.)

It looks like I had already saved all of the Order objects before adding the 'order_date' attribute, so when I saved them again w/ the 'order_date' attribute I could only see the initial Order objects that didn't have it from the original save in the console. Oops.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.