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

Polymorphism: What is <relation>_field?

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

      In specifying an objects polymorphic relation (say :object), three fields are being created: object_type, object_field, object_id.

      type and id are typical but I've never seen field before and was just wondering what it is there for? (I don't see any use)

      I did the following in a rails (3.2.11) console that has a mongoid (3.1.2) database

      1.9.3-p194 :009 > class TestObject
      1.9.3-p194 :010?>   include Mongoid::Document
      1.9.3-p194 :011?>   belongs_to :object, polymorphic: true
      1.9.3-p194 :012?> end
       => #<Mongoid::Relations::Metadata ... >
      1.9.3-p194 :013 > TestObject.new
       => #<TestObject _id: 518d7d75330eb976d2000002, object_type: nil, object_field: nil, object_id: nil>
      1.9.3-p194 :014 > TestObject.new( object: TestObject.create )
       => #<TestObject _id: 518d7de5330eb976d2000004, object_type: "TestObject", object_field: nil, object_id: "518d7de5330eb976d2000003"> 
      

            Assignee:
            durran Durran Jordan
            Reporter:
            charlierudolph charlierudolph
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: