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

Issue with multiple has_many pointing using the same class

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

      Mongoid 2.4.9

      Since upgrading from 2.4 to 2.4.9 I've started seeing an issue where assigning via the relationship of a belongs_to when there are two pointed at the same class, overwrites the first one's ID

      class User
      has_many :created_posts, :class_name => "Post", :inverse_of => :creator, :validate => false
      has_many :assigned_posts, :class_name => "Post", :inverse_of => :assignee, :validate => false
      end

      class Post
      belongs_to :creator, :class_name => "User"
      belongs_to :assignee, :class_name => "User"
      end

      ruby-1.9.2-head :004 > u1 = User.create
      => #<User _id: 4fac35f0803544465b000001, _type: "User", ...>
      ruby-1.9.2-head :005 > u2 = User.create
      => #<User _id: 4fac35fe803544465b000002, _type: "User", ...>
      ruby-1.9.2-head :006 > p = Post.new
      => #<Post _id: 4fac3602803544465b000003, _type: nil, ...>
      ruby-1.9.2-head :007 > p.creator = u1
      => #<User _id: 4fac35f0803544465b000001, _type: "User", ...>
      ruby-1.9.2-head :009 > p.assignee = u2
      => #<User _id: 4fac35fe803544465b000002, _type: "User", ...>
      ruby-1.9.2-head :010 > p.creator_id
      => BSON::ObjectId('4fac35fe803544465b000002') // <= notice this is the incorrect id
      ruby-1.9.2-head :011 > p.assignee_id
      => BSON::ObjectId('4fac35fe803544465b000002')
      ruby-1.9.2-head :012 > p.creator.id
      => BSON::ObjectId('4fac35f0803544465b000001') // <= although this one is correct

            Assignee:
            Unassigned Unassigned
            Reporter:
            nader Nader Akhnoukh
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: