如何解决`对象不支持#inspect`错误?

时间:2012-04-21 00:36:21

标签: ruby-on-rails ruby ruby-on-rails-3 inspect

我正在使用rails v3.2.2,当我尝试加载相关记录时,我收到一个奇怪的错误。

以下是我得到的终端输入/输出:

1.9.2-p318 :011 > Category.first
=> #<Category id: 1, ...>

1.9.2-p318 :013 > Category.first.articles
  Article Load (0.2ms)  SELECT `articles`.* FROM `articles` LIMIT 1
(Object doesn't support #inspect)

1.9.2-p318 :014 > Category.first.articles.first
  Category Load (0.2ms)  SELECT `categories`.* FROM `categories` LIMIT 1
NoMethodError: undefined method `scoped' for Category::Article:Module
    from /<USER_PATH>/.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.2/lib/active_record/associations/association.rb:123:in `target_scope'
    from /<USER_PATH>/.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.2/lib/active_record/associations/through_association.rb:15:in `target_scope'
    from /<USER_PATH>/.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.2/lib/active_record/associations/association.rb:87:in `scoped'
    from /<USER_PATH>/.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.2/lib/active_record/associations/collection_association.rb:569:in `first_or_last'
    from /<USER_PATH>/.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.2/lib/active_record/associations/collection_association.rb:101:in `first'
    from /<USER_PATH>/.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.2/lib/active_record/associations/collection_proxy.rb:46:in `first'
    from (irb):14
    from /<USER_PATH>/.rvm/gems/ruby-1.9.2-p318/gems/railties-3.2.2/lib/rails/commands/console.rb:47:in `start'
    from /<USER_PATH>/.rvm/gems/ruby-1.9.2-p318/gems/railties-3.2.2/lib/rails/commands/console.rb:8:in `start'
    from /<USER_PATH>/.rvm/gems/ruby-1.9.2-p318/gems/railties-3.2.2/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

Category模型中,我有:

class Category < ActiveRecord::Base
  has_many :article_relationships,
    :class_name  => 'Category::Article::ArticleRelationship',
    :foreign_key => 'category_id'

  has_many :articles,
    :through     => :article_relationships,
    :source      => :article
end

Category::Article::ArticleRelationship我有:

class Category::Article::ArticleRelationship < ActiveRecord::Base
  belongs_to :article,
    :class_name    => 'Article',
    :foreign_key   => 'article_id'
end

如何解决与Object doesn't support #inspect相关的问题?


注意:在同一个Category模型中,我有类似Category::Article::ArticleRelationship的声明(它与User类通过{{1}相关它不会导致问题。

3 个答案:

答案 0 :(得分:5)

您的应用中有两个名为Article的常量。一个是你的活跃记录类,顶级常量。另一个是模块 Category::Article

执行belongs_to :article时,看起来rails开始在类中调用Article常量,因为它找到了错误的一个。这会造成各种各样的混乱,因为你显然不能互换地使用activerecord类和模块

设置:class_name => '::Article'会强制查找顶级Article类。

答案 1 :(得分:5)

如果使用rails 4,问题来自protected_attributes gem,你需要将gem版本1.0.3升级到1.0.5,然后它才能工作。

答案 2 :(得分:0)

问题似乎是通过在我的Category::Article::ArticleRelationship中声明以下内容来解决的:

class Category::Article::ArticleRelationship < ActiveRecord::Base
  belongs_to :article,
    :class_name    => '::Article', # note I added '::'
    :foreign_key   => 'article_id'
end

但我不明白为什么?