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

Problem with relations pointing the same model

    XMLWordPrintable

    Details

    • Type: Task
    • Status: Closed
    • Resolution: Done
    • Affects Version/s: None
    • Fix Version/s: 2.4.10
    • Component/s: None
    • Labels:

      Description

      I have this model:

      class Match
        include Mongoid::Document
        include Mongoid::Timestamps
        field :user1_id, :type => BSON::ObjectId
        field :user2_id, :type => BSON::ObjectId
       
        belongs_to :user1, :class_name => "User", :foreign_key => "user1_id"
        belongs_to :user2, :class_name => "User", :foreign_key => "user2_id"
      end
      

      `

      And User model like this:

      class User
        include Mongoid::Document
        include Mongoid::Timestamps
       
        has_many :hosted_matches, :class_name => "Match", :foreign_key => "user1"
        has_many :invited_matches, :class_name => "Match", :foreign_key => "user2"
      end
      

      `

      All cool, but when doing this:

      @match=Match.new
      @match.user1 = @user1
      @match.user2 = @user2
      @match.save!
      

      `

      After saving, user1 and user2 will point to the same user...

      The only way I got this running properly is by setting ids:

      @match=Match.new
      @match.user1_id = @user1.id
      @match.user2_id = @user2.id
      @match.save!
      

      `

        Attachments

          Activity

            People

            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: