factory_bot icon indicating copy to clipboard operation
factory_bot copied to clipboard

FactoryGirl strips leading 0 off literal strings if it think they are numbers

Open kevinmccaughey opened this issue 12 years ago • 4 comments

The following simple object gets the leading 0 (zero) from the mobile string removed when a factory is created:

FactoryGirl.define do factory :user do first_name 'Kevin' last_name 'McCaughey' email '[email protected]' password 'password' password_confirmation 'password' pin '1234' mng_level '100' staff_id 'boss' mobile '07931911911' mobile_check false end end

Inspecting the mobile (after and instance of user is created using factories) gives "7931911911" - where'd the leading zero go?!?!?!?!

If I instead change mobile to 'frustratedbeyondbelief' inspection of mobile gives 'frustratedbeyondbelief' as expected.

I am inspecting using a before_validation when it unexpectedly failed validation of my mobile number (mobile numbers in the UK start with a zero).

Somewhere, somehow, FG is converting my string literal to a number, then back to a string with the leading zero removed!!

Can someone tell me what on earth is going on here please? I couldn't believe it when I eventually found it out, after about 4 hours of drilling.

And before you ask.... at NO POINT do I ever try and change or remove the leading zero in any of my validation code or otherwise.

Many thanks,

Kevin

kevinmccaughey avatar Mar 18 '14 17:03 kevinmccaughey

@kevinmccaughey can you look at db/schema.rb and let me know the type for mobile on the users table?

joshuaclayton avatar Mar 18 '14 18:03 joshuaclayton

create_table "users", force: true do |t| t.string "first_name" t.string "last_name" t.string "email" t.string "staff_id" t.string "pin" t.integer "mng_level" t.datetime "created_at" t.datetime "updated_at" t.integer "team_id" t.string "password_digest" t.string "pin_digest" t.string "mobile" t.boolean "mobile_check" end

add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree add_index "users", ["mobile"], name: "index_users_on_mobile", using: :btree

It's definitely a string - I have inspected it with pg_admin as well ;)

EDIT: It's the strangest thing - I have been sitting here scratching my head about it for hours trying to guess what it could possibly be - definitely my weirdest programming discovery for a few years ;) Thanks for helping.

kevinmccaughey avatar Mar 18 '14 18:03 kevinmccaughey

Actually this is (probably) not FG's fault at all. I have replicated it just using a form input and it seems that even though it is a text field input in the form, it strips the leading zero. It doesn't do this when Capybara fills it in though - only when manual and using a factory. I will leave open a little while longer just so I can confirm this, then close with an answer.

kevinmccaughey avatar Mar 19 '14 10:03 kevinmccaughey

Ok, this is my fault and very confusing. The explanation is that I was using the following code right before_save and had to add .dup in order that the code did not actually manipulate the original string. So I originally had:

number = self.mobile number = mobile_convert_intl(number)

This actually meant that self.mobile was changed, even though I had expected only number to change - no idea why. The solution was:

number = self.mobile.dup number = mobile_convert_intl(number)

I am posting it here for posterity - who knows, some poor sod might come up with the same problem and blame FG like I had ;)

EDIT: And here is why (elementary Ruby, but I am new): http://patshaughnessy.net/2012/1/18/seeing-double-how-ruby-shares-string-values

kevinmccaughey avatar Mar 19 '14 11:03 kevinmccaughey