How to solve the `Object doesn't support #inspect` error?

前端 未结 3 1729
傲寒
傲寒 2020-12-20 05:34

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         


        
相关标签:
3条回答
  • 2020-12-20 06:25

    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.

    0 讨论(0)
  • 2020-12-20 06:30

    The problem seems to be solved by stating the following in my Category::Article::ArticleRelationship:

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

    but I didn't understand why?

    0 讨论(0)
  • 2020-12-20 06:37

    If are using rails 4, the problem came from protected_attributes gem, u need to upgrade the gem version 1.0.3 to 1.0.5, then it will work.

    0 讨论(0)
提交回复
热议问题