ActiveRecord to_i method removed in Rails 3.2.9

Updated to Rails 3.2.9 today from 3.2.7, and it seems that the "to_i" method has been removed from ActiveRecord.

Is it for design? Or is it a mistake? I cannot find mention of this in the change notes. This will affect a lot of code.

thanks!

+7
source share
6 answers

The #to_i method never existed, but the way objects are assigned to values โ€‹โ€‹has been changed in Rails 3.2.8 .

Given the following:

 class Lecture < ActiveRecord::Base has_one :professor end 

Previously, you could appoint a professor for such a lecture:

 @lecture.professor = Professor.find_by_id(1) 

Or like this:

 @lecture.professor_id = Professor.find_by_id(1) 

In the first case, everything is simple, because the professor association expects the professor 's object. In the second case, although ActiveRecord did some magic to force the id from the professor, because professor_id expects an integer.

In Rails 3.2.8 and later, this magic no longer works. This is nice in a way, as it is a hint that you are probably doing something wrong. For example, if there is no professor_id column in the database, but just professor who expects an integer, this type of assignment will no longer work.

It looks like this will be reverted to the previous behavior in Rails 3.2.11 .

+4
source

There was one change in Rails 3.2.9 regarding the to_i method, but this applies to columns, not tables. See here .

Thus, in your code, if you passed the whole object instead of the column value, it was silent before. But now it actually calls the to_i method on the object and will throw an exception.

+2
source

I encountered the same error. This is not that ActiveRecord is missing #to_i, but something else that in my case is related to the factory_girl stone.

 factory_girl (4.1.0) factory_girl_rails (4.1.0) 

Here's an isolated case of difference:

 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> 

Pay attention to nil user_id for Rails 3.2.11

+2
source

I do not believe that the to_i method had an event available on AR objects. Could you be more specific regarding the earlier use of the method, which now fails?

0
source

To keep track of Luca, I use Rails 3.2.11, and I'm not sure I have this code, but I found that it no longer works:

 @lecture.update_attribute(:professor_id, Professor.find_by_id(1)) 
0
source

I also had the same problem when I upgrade rails of version 3.2.3 to rails 3.2.11.

Here are some snippets of code exactly what the problem is

In rails 3.2.3
doctor = Doctor.First
patient = Patient.new
patient.doctor_id = doctor (Right side Object assignment is valid)
patient.save! (Valid)

In rails 3.2.11
doctor = Doctor.First
patient = Patient.new
patient.doctor_id = doctor (Right side Object assignment is invalid)
patient.save! (Not valid)
NoMethodError: undefined method 'to_i' error, we got such assignments.

CONCLUSION WE ARE USED TO USE _ID = INTEGER ONLY not only as an assignment of objects.
More details

0
source

All Articles