Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

NoMethodError .to_i when upgrading to Rails 3.2.11 #473

Closed
ultrasaurus opened this Issue · 6 comments

6 participants

@ultrasaurus

I don't know that this is a bug, per se, but it is a change in behavior in upgrading from 3.1.0 to 3.2.11. Here's an isolated case:

rails _3.1.0_ new rails31
cd rails31
bundle install
rails g scaffold note title:string contents:text
rails g scaffold user name:string
rails g migration add_user_id_to_notes user_id:integer
rake db:migrate

Then in the console:

require 'factory_girl'
FactoryGirl.define { factory :note }
Factory(:note, :user_id => User.first, :title => 'foo')
  User Load (0.1ms)  SELECT "users".* FROM "users" LIMIT 1
  SQL (0.5ms)  INSERT INTO "notes" ("contents", "created_at", "title", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?)  [["contents", nil], ["created_at", Sun, 13 Jan 2013 21:29:36 UTC +00:00], ["title", "foo"], ["updated_at", Sun, 13 Jan 2013 21:29:36 UTC +00:00], ["user_id", 1]]
 => #<Note id: 3, title: "foo", contents: nil, created_at: "2013-01-13 21:29:36", updated_at: "2013-01-13 21:29:36", user_id: 1> 

However, if I do the same sequence of steps with:

rails _3.2.11_ new rails3211

Then in the console I see:

1.9.2-p290 :001 > require 'factory_girl'
 => true 
1.9.2-p290 :002 > FactoryGirl.define { factory :note }
 => [] 
1.9.2-p290 :003 > Factory(:note, :user_id => User.first, :title => 'foo')
  User Load (0.1ms)  SELECT "users".* FROM "users" LIMIT 1
   (0.0ms)  begin transaction
  SQL (24.5ms)  INSERT INTO "notes" ("contents", "created_at", "title", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?)  [["contents", nil], ["created_at", Sun, 13 Jan 2013 21:33:03 UTC +00:00], ["title", "foo"], ["updated_at", Sun, 13 Jan 2013 21:33:03 UTC +00:00], ["user_id", nil]]
   (2.4ms)  commit transaction
 => #<Note id: 1, title: "foo", contents: nil, created_at: "2013-01-13 21:33:03", updated_at: "2013-01-13 21:33:03", user_id: nil> 

Note the nil user_id for Rails 3.2.11

I tried upgrading to latest FactoryGirl (4.1.0) but saw no change in behavior.

@rizidoro

I have the same issue...

@joshuaclayton

@ultrasaurus @rizidoro if you remove FactoryGirl from this, do you still see the issue?

note = Note.new
note.user_id = User.first
note.title = 'foo'
note.save!
note.reload
puts "user_id is: #{note.user_id}"

Let me know what you see!

@valikos

@ultrasaurus @rizidoro @joshuaclayton
I think right write in rails 3.2.11 like this

note = Note.new
note.user_id = User.first.id
...
@mabahamo

:+1:

There is a regression bug on Rails 3.2.11 : rails/rails#8832

@joshuaclayton

Thanks for pointing this out, @mabahamo!

@descentintomael

This is not a regression bug. Previous Rails versions had a weird issue where when it would try to convert a value to an integer it would default to 1 or 0 depending on the presence of the value. @valikos has it right, you need to call the id method to get the value you want.

@ultrasaurus, the reason this worked for you before is that you were getting the first user whose id just happened to be 1. I ran into the same problem in one of my codebases when I upgraded.

For reference to what the old code was doing, see this line from 3.2.8:
https://github.com/rails/rails/blob/v3.2.8/activerecord/lib/active_record/connection_adapters/column.rb#L78

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.