简体   繁体   中英

Rails_admin undefined method `associations' for nil:NilClass

I have these models:

Class A 
   embeds_many :b
end

Class B
   belongs_to :c
end

Class C
end

I'm working with rails_admin and mongoid. In admin, when I try to retrieve the list of C records when I'm creating an A instance I'm getting this error:

This only happens on production envirnment not in development

NoMethodError (undefined method `associations' for nil:NilClass):
      /home/pablo/.rvm/gems/ruby-2.3.0@mh-backend/bundler/gems/rails_admin-355dc80f8a20/lib/rails_admin/adapters/mongoid/abstract_object.rb:10:in `initialize'
      /home/pablo/.rvm/gems/ruby-2.3.0@mh-backend/bundler/gems/rails_admin-355dc80f8a20/lib/rails_admin/adapters/mongoid.rb:24:in `new'
      /home/pablo/.rvm/gems/ruby-2.3.0@mh-backend/bundler/gems/rails_admin-355dc80f8a20/lib/rails_admin/adapters/mongoid.rb:24:in `get'
      /home/pablo/.rvm/gems/ruby-2.3.0@mh-backend/bundler/gems/rails_admin-355dc80f8a20/app/controllers/rails_admin/main_controller.rb:138:in `get_association_scope_from_params'

Taking a look at rails_admin code we can see that piece of code in mongoid.rb file.

    def get(id)
        AbstractObject.new(model.find(id))
      rescue => e
        raise e if %w(
          Mongoid::Errors::DocumentNotFound
          Mongoid::Errors::InvalidFind
          Moped::Errors::InvalidObjectId
          BSON::InvalidObjectId
        ).exclude?(e.class.to_s)
      end

If we pay attention at this code we can see that model.find(id) must produce a Mongoid::Errors::DocumentNotFound if document doesn't exists by default.

However, in mongoid you can avoid the raise of this error with raise_not_found_error: true in mongo config file, this produce the undefined method of nil class.

Tracking issue on github

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM