【问题标题】:How to solve the `Object doesn't support #inspect` error?如何解决“对象不支持#inspect”错误?
【发布时间】:2012-04-21 00:36:20
【问题描述】:

我正在使用 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 的类似声明(它通过 Category::UserRelationship 类与 User 类相关)但它没有造成问题。

【问题讨论】:

  • 在尝试检查它们之前您是否require 'article'?
  • @sarnold - 不,我根本没有require 'article'。

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


【解决方案1】:

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

当您执行belongs_to :article 时,似乎rails 开始在调用belongs_to 的类中寻找Article 常量,因此它找到了错误的常量。这会导致各种混乱,因为您显然不能互换使用 activerecord 类和模块

设置:class_name =&gt; '::Article' 会强制找到顶级Article 类。

【讨论】:

  • 一个类和一个模块同名是“危险的”吗?如果是这样,还有什么更好的方法?
  • 无论如何,我刚刚意识到我有两个 类(其中一个是命名空间的)可以符合您所说的:Category 和 Category::Article。
  • 不危险,有时您只需要消除歧义。
  • 我有一些问题我打开了一个相关问题:stackoverflow.com/questions/10255587/…
【解决方案2】:

如果使用的是rails 4,问题来自于protected_attributes gem,你需要将gem版本1.0.3升级到1.0.5,然后它就可以工作了。

【讨论】:

    【解决方案3】:

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

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

    但我不明白为什么?

    【讨论】:

      猜你喜欢
      • 2011-12-03
      • 2015-03-03
      • 2021-10-29
      • 1970-01-01
      • 2015-01-09
      • 1970-01-01
      • 2012-06-10
      • 1970-01-01
      • 2023-02-06
      相关资源
      最近更新 更多