0

I am doing some maintenance on a Ruby on Rails application running Ruby 1.8.7 and Rails 2.3.5. The database used is MySQL.

I am encountering a very strange bug on one of my models. Before saving, the id is nil (since it hasn't been assigned yet by saving to db). Then, when I do @model.save it saves the record in the db, but when I call @model.id it returns a negative value (-294731885) instead of the id assigned to it in the db (4000235410).

To debug, I have added the following to my shopping_cart model:

before_save :print_object_before
after_save :print_object_after

def print_object_before
  logger.debug "ShoppingCartBeforeSave: " + self.inspect
end

def print_object_after
  logger.debug "ShoppingCartAfterSave: " + self.inspect
end

In my controller, I call:

@shopping_cart.save!

The output I get in the log is as follows:

ShoppingCartBeforeSave: #<ShoppingCart id: nil, user_id: 4000001127, created_at: nil, updated_at: nil, currency_code: "zar", coupon_code: nil>
ShoppingCartAfterSave: #<ShoppingCart id: -294731885, user_id: 4000001127, created_at: "2012-09-25 12:02:05", updated_at: "2012-09-25 12:02:05", currency_code: "zar", coupon_code: nil>

What could possibly be the problem? There are no other before- or after-filters defined on the model. I thought I might have run out of id's on MySQL, but that is not the case since it still saves the record correctly (and with a positive id) in the database despite returning a different value in rails. (For information, in the migration for the model, id is defined as integer.)

Some more info: it seems like the value of the negative value keeps increasing (smaller negative), eg on one attempt id would contain -294731885 on next attempt it would contain -294731884, on next attempt it would contain -294731883, etc.

3
  • What is the data type of your id column? Sep 25, 2012 at 12:31
  • As mentioned, it is defined as integer. Which, as I understand it, means that it defaults to integer(4).
    – Stanley
    Sep 25, 2012 at 12:33
  • Sorry, I missed that at the end of the question. Sep 25, 2012 at 12:34

1 Answer 1

0

I'm not sure of this, but you might have an issue with the type of your id column, which seems to not be large enough to hold the numbers you're getting at. Maybe the issue is on the ruby side though. I cannot be sure since I don't have the right versions installed and not enough experience with relational databases. hope this helps !

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.