'对象不支持Rails 4.1.1中使用包含的#inspect'

时间:2014-05-12 16:37:56

标签: ruby-on-rails ruby-on-rails-4.1

我刚刚从Rails 4.0.2升级到Rails 4.1.1,并且在尝试在ActiveRecord查询中使用includes时,我看到了一些奇怪的行为。我在Windows 7,64位开发环境中。我定义了以下模型:

class Article < ActiveRecord::Base
    belongs_to :intro_image, :class_name => "UploadedFile"
    belongs_to :user
end

class User < ActiveRecord::Base
    has_many :articles
end

class UploadedFile < ActiveRecord::Base
    has_many :articles, :foreign_key => :intro_image_id, :dependent => :nullify
end

当我打开控制台并执行以下命令时,它们按预期工作

Article.count   # => 19 records
Article.includes(:user)   # => 19 records returned and I see the load call on the User table

然而,执行以下命令,

Article.includes(:intro_image)

在控制台中生成以下输出,

Article Load (1.0ms)  SELECT `articles`.* FROM `articles`
UploadedFile Load (1.0ms)  SELECT `uploaded_files`.* FROM `uploaded_files`  WHERE `uploaded_files`.`id` IN (3, 4, 6, 8, 0, 9, 13, 15, 40, 48, 49, 50, 51, 56, 55, 53, 54, 52)
(Object doesn't support #inspect)

为什么此调用会生成Object doesn't support #inspect消息?另外,如果我执行以下命令:

Article.includes(:intro_image).to_a

我得到以下堆栈跟踪

NoMethodError: undefined method `each' for nil:NilClass
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/activerecord-4.1.1/lib/active_record/associations/preloader/association.rb:87:in `block in associated_records_by_owner'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/activerecord-4.1.1/lib/active_record/associations/preloader/association.rb:86:in `each'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/activerecord-4.1.1/lib/active_record/associations/preloader/association.rb:86:in `associated_records_by_owner'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/activerecord-4.1.1/lib/active_record/associations/preloader/singular_association.rb:9:in `preload'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/activerecord-4.1.1/lib/active_record/associations/preloader/association.rb:20:in `run'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/activerecord-4.1.1/lib/active_record/associations/preloader.rb:136:in `block (2 levels) in preloaders_for_one'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/activerecord-4.1.1/lib/active_record/associations/preloader.rb:134:in `each'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/activerecord-4.1.1/lib/active_record/associations/preloader.rb:134:in `map'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/activerecord-4.1.1/lib/active_record/associations/preloader.rb:134:in `block in preloaders_for_one'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/activerecord-4.1.1/lib/active_record/associations/preloader.rb:133:in `each'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/activerecord-4.1.1/lib/active_record/associations/preloader.rb:133:in `flat_map'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/activerecord-4.1.1/lib/active_record/associations/preloader.rb:133:in `preloaders_for_one'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/activerecord-4.1.1/lib/active_record/associations/preloader.rb:106:in `preloaders_on'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/bullet-4.9.0/lib/bullet/active_record41.rb:34:in `preloaders_on'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/activerecord-4.1.1/lib/active_record/associations/preloader.rb:94:in `block in preload'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/activerecord-4.1.1/lib/active_record/associations/preloader.rb:93:in `each'
... 5 levels...
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/activerecord-4.1.1/lib/active_record/relation.rb:487:in `load'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/activerecord-4.1.1/lib/active_record/relation.rb:231:in `to_a'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/bullet-4.9.0/lib/bullet/active_record41.rb:10:in `to_a'
from (irb):13
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.1.1/lib/rails/commands/console.rb:90:in `start'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.1.1/lib/rails/commands/console.rb:9:in `start'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.1.1/lib/rails/commands/commands_tasks.rb:69:in `console'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.1.1/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.1.1/lib/rails/commands.rb:17:in `<top (required)>'
from C:/Users/Tom/Documents/dev/site/site_source/bin/rails:4:in `require'
from C:/Users/Tom/Documents/dev/site/site_source/bin/rails:4:in `<top (required)>'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/ruby-debug-ide-0.4.23.beta1/lib/ruby-debug-ide.rb:86:in `debug_load'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/ruby-debug-ide-0.4.23.beta1/lib/ruby-debug-ide.rb:86:in `debug_program'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/ruby-debug-ide-0.4.23.beta1/bin/rdebug-ide:110:in `<top (required)>'
from -e:1:in `load'
from -e:1:in `<main>'

1 个答案:

答案 0 :(得分:0)

SELECT `uploaded_files`.* FROM `uploaded_files`  WHERE `uploaded_files`.`id` IN (3, 4, 6, 8, 0, 9, 13, 15, 40, 48, 49, 50, 51, 56, 55, 53, 54, 52)

数据库中uploaded_filesid的类型是varchar?