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

inverse_of => nil on has_and_belongs_to_many incorrectly creates inverse relationship

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

      Using:

      • mongo (1.4.0)
      • mongoid (2.3.0)

      I've got the following model:

      class Blog
        include Mongoid::Document
        include Mongoid::Timestamps
        
        belongs_to :user
        has_many :post
        has_and_belongs_to_many :blogs, :inverse_of => nil
        
        validates_presence_of :title
        
        field :title, type: String
        field :url, type: String
        field :private, type: Boolean
        field :password, type: String
      end
      

      Yet when i do the following:

      b1 = Blog.new(title: "blog1")
      b1.save
      b2 = Blog.new(title: "blog2")
      b2.save
      b1.blogs << b2
      

      Both blogs reference each other instead of what I expect which is just b1 having a reference to b2 and b2 not having a reference to b1.

      Good:

      {
      	"_id" : ObjectId("4e8c99f984a080493c000001"),
      	"blog_ids" : [
      		ObjectId("4e8c9a1484a080493c000002")
      	],
      	"created_at" : ISODate("2011-10-05T17:55:11Z"),
      	"title" : "blog1",
      	"updated_at" : ISODate("2011-10-05T17:55:11Z")
      }
      

      Bad:

      {
      	"_id" : ObjectId("4e8c9a1484a080493c000002"),
      	"blog_ids" : [
      		ObjectId("4e8c99f984a080493c000001")
      	],
      	"created_at" : ISODate("2011-10-05T17:55:35Z"),
      	"title" : "blog2",
      	"updated_at" : ISODate("2011-10-05T17:56:08Z")
      }
      

            Assignee:
            Unassigned Unassigned
            Reporter:
            TylerBrock TylerBrock [X]
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: