简体   繁体   中英

NoMethodError: private method `existence_check' called for Class

I have a very strange problem.

Scenario : I'm working on a collection of photo in Rails with MongoDB (mongoid gem), where each photo belongs to an album. Some mongo fields are provided by concerns and models are correctly namespaced. So,

Concerns: MediaConcerns::Base

module MediaConcerns
  module Base
    extend ActiveSupport::Concern

    included do
      field :photo_description, type: String
    end
  end
end

Concerns: MediaCollectionConcerns::Base

module MediaCollectionConcerns
  module Base
    extend ActiveSupport::Concern

    included do
      field :album_title, type: String
      field :album_description, type: String
    end
  end
end

And MediaType::Photo is:

module MediaType
  class Photo
    include Mongoid::Document
    include Mongoid::Timestamps::Short
    include MediaConcerns::Base

    embedded_in :photo_album, class_name: 'MediaCollection::PhotoAlbum'    
  end
end

While MediaCollection::PhotoAlbum is:

module MediaCollection
  class PhotoAlbum
    include Mongoid::Document
    include Mongoid::Timestamps::Short
    include MediaCollectionConcerns::Base

    embeds_many :photos, class_name: 'MediaType::Photo'
  end
end

With concerns or not, the problem persist, and is related to the embedded_in and embeds_many .

By Rails console, when I try MediaCollection::PhotoAlbum.new it gives:

NoMethodError: private method `existence_check' called for MediaCollection::PhotoAlbum:Class
from /home/marco/.rvm/gems/ruby-2.4.0/gems/mongoid-6.1.0/lib/mongoid/relations/macros.rb:355:in `relate'
from /home/marco/.rvm/gems/ruby-2.4.0/gems/mongoid-6.1.0/lib/mongoid/relations/macros.rb:85:in `embeds_many'
from /home/marco/workspace/backend/app/models/media_collection/photo_album.rb:10:in `<class:PhotoAlbum>'
from /home/marco/workspace/backend/app/models/media_collection/photo_album.rb:2:in `<module:MediaCollection>'
from /home/marco/workspace/backend/app/models/media_collection/photo_album.rb:1:in `<top (required)>'

And the same for MediaType::Photo.new :

NoMethodError: private method `existence_check' called for MediaType::Photo:Class
from /home/marco/.rvm/gems/ruby-2.4.0/gems/mongoid-6.1.0/lib/mongoid/relations/macros.rb:355:in `relate'
from /home/marco/.rvm/gems/ruby-2.4.0/gems/mongoid-6.1.0/lib/mongoid/relations/macros.rb:56:in `embedded_in'
from /home/marco/workspace/backend/app/models/media_type/photo.rb:15:in `<class:Photo>'
from /home/marco/workspace/backend/app/models/media_type/photo.rb:2:in `<module:MediaType>'
from /home/marco/workspace/backend/app/models/media_type/photo.rb:1:in `<top (required)>'

What's the problem? If I remove (for debug only) the relationships embeds_many and embedded_in , the problem is solved, but i need to embed those documents!

Maybe Rails can't find the :photos , but I've specified class_name: 'MediaType::Photo' as mongoid documentation suggests, and the same is specified for 'MediaCollection::PhotoAlbum'

I need help. Thanks.

I finally found the reason but not the solution, is a bug on the gem.

Read here: github issue

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