如何解决`Object不支持#inspect`错误? [英] How to solve the `Object doesn't support #inspect` error?

查看:114
本文介绍了如何解决`Object不支持#inspect`错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 rails v3.2.2,当我尝试加载相关记录时出现奇怪的错误.

I am using rails v3.2.2 and I get a strange error when I try to load associated records.

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

The following is the Terminal input/output I get:

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 模型中,我有:

In my Category model I have:

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不支持#inspect的相关问题?

How to solve the problem related to Object doesn't support #inspect?

注意:在同一个 Category 模型中,我有一个类似于 Category::Article::ArticleRelationship 的类似语句(它与User 类到 Category::UserRelationship 类),它不会引起问题.

Note: In the same Category model I have a similar statement like for Category::Article::ArticleRelationship (it is related to an User class through a Category::UserRelationship class) and it does not cause problems.

推荐答案

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

There are two constants called Article on your app. One is your active record class, top level constant. The other is the module Category::Article.

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

When you do belongs_to :article, it would seem that rails starts looking for an Article constant in the class the belongs_to is called from so it is finding the wrong one. This causes all sorts of mess, since you obviously can't use an activerecord class and a module interchangeably

设置 :class_name =>'::Article' 强制找到顶级 Article 类.

Setting :class_name => '::Article' forces the top level Article class to be found instead.

这篇关于如何解决`Object不支持#inspect`错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆