Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.0k views
in Technique[技术] by (71.8m points)

ruby on rails - How to solve the `Object doesn't support #inspect` error?

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>'

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

In my Category::Article::ArticleRelationship I have:

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

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


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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...