2

我正在使用 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模型中,我有一个类似的语句,比如 for Category::Article::ArticleRelationship(它User通过一个Category::UserRelationship类与一个类相关)并且它不会引起问题。

4

3 回答 3

5

您的应用程序调用了两个常量Article。一个是您的活动记录类,顶级常量。另一个是模块 Category::Article

当你这样做belongs_to :article时,似乎 rails 开始Article在类中寻找一个常量,从那里调用 belongs_to,所以它找到了错误的。这会导致各种混乱,因为您显然不能互换使用 activerecord 类和模块

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

于 2012-04-21T01:16:28.710 回答
5

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

于 2014-04-09T12:38:30.280 回答
0

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

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

但我不明白为什么?

于 2012-04-21T01:06:09.820 回答