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

REFERENCED ONE TO ONE default dependent behavior

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

      The docs say that the default dependent behavior for a referenced one to one relationship ( i.e. has_one and belongs_to ) should be to nullify the parent id field of the object being replaced by an assignment.

      This does not seem to be the case. In this console example User has_one MedicalProfile

      ruby-1.9.2-p180 :001 > u = User.first
       => #<User _id: 4e6e6b002b6df12ae4000012,...>
      
      ruby-1.9.2-p180 :003 > mp = u.medical_profile
       => #<MedicalProfile _id: 4e6e6df12b6df12732000004,...>
      
      ruby-1.9.2-p180 :004 > u.medical_profile = MedicalProfile.create
       => #<MedicalProfile _id: 4e6ec0aa2b6df175bb000001,...>
      
      ruby-1.9.2-p180 :005 > mp
       => #<MedicalProfile _id: 4e6ec0aa2b6df175bb000001,...> 
      
      ruby-1.9.2-p180 :007 > old_mp = MedicalProfile.find '4e6ec0aa2b6df175bb000004'
      Mongoid::Errors::DocumentNotFound: Document not found for class MedicalProfile with id(s) 4e6ec0aa2b6df175bb000004.
      

      Also, notice that after I call u.medical_profile = MedicalProfile.create, the reference variable mp I created earlier which should still point at the original medical_profile object now seems to point at the medical_profile object created by the call to u.medical_profile = MedicalProfile.create.

      This seems wrong. Is the id object of the same in memory object just being swapped out when the assignment is made?

            Assignee:
            Unassigned Unassigned
            Reporter:
            jesseclark Jesse Clark
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: