Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-3961

embeds_one NameError: uninitialized constant

    • Type: Icon: Task Task
    • Resolution: Done
    • 12_01_17
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None

      Following the documentation: http://mongoid.org/en/mongoid/docs/relations.html#embeds_one
      I get the following error when I try to define an embeds_one relationship:

      NameError: uninitialized constant Apikey

      This is the code:

      class ApiKey
        include Mongoid::Document
        
        field :access_token, type: String 
        embedded_in :user
        before_create :generate_access_token
        
        private
          def generate_access_token
            self.access_token = SecureRandom.hex
          end
      end
      
      class User 
        include Mongoid::Document
      
        field :name, type: String
        embeds_one :apikey
      end
      
      u = User.new name: 'foo'
      a = ApiKey.new
      
      puts u.inspect 
      #<User _id: 54ecd47b6c6f63241d000000, name: "foo">
      
      puts a.inspect
      #<ApiKey _id: 54ecdade6c6f6324fc010000, access_token: nil>
      
      u.apikey = a   # Here raise the error!
      

            Assignee:
            Unassigned Unassigned
            Reporter:
            Vrael Vrael [X]
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: