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

identity :type => Integer and string attributes

    • Type: Icon: Task Task
    • Resolution: Done
    • 2.3.4
    • Affects Version/s: None
    • Component/s: None

      This works:

      class Library
        include Mongoid::Document
        identity :type => Integer
      end
      class Book
        include Mongoid::Document  
        belongs_to :library
      end
      
      Library.delete_all
      Book.delete_all
      
      library = Library.create!(:id => 1)
      Book.create!(:library_id => 1)
      p Book.first.library
      

      prints

      #<Library _id: 1, _type: nil>
      

      But if you change it to this:

      Book.create!(:library_id => "1")
      

      It fails:

      nil
      

      Why does this matter? Oftentimes in Rails you use params to create/update Mongoid objects. Params are always strings. So if you change your identity type from BSON::ObjectId to Integer, things suddenly stop working. You can workaround the bug by using callbacks to set the correct type, but that seems like a hack.

            Assignee:
            durran Durran Jordan
            Reporter:
            gurgeous Adam Doppelt
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: