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

Bug with relations in mongoid 2.1.2

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

      We did something similar to below example with mongoid 2.0.x and had no problems - with mongoid 2.1.2 (didn't test other 2.1.x releases) relations aren't saved. I can't see anything wrong with my code.

      require 'rubygems'
      require 'ruby-debug'
      gem "mongoid", "= 2.1.2" ## swap for 2.0.2 to make this example work
      require 'mongoid'

      DB = 'mongoid_failing_example_db'
      $conn = Mongo::Connection.new
      $conn.drop_database(DB)

      Mongoid.configure do |config|
      config.master = $conn.db(DB)
      end

      class Person
      include Mongoid::Document
      include Mongoid::Timestamps
      field :firstname
      field :lastname

      belongs_to :family

      after_create :create_relations

      def create_relations
      family = Family.first(:conditions => { :name => self.lastname })
      if family.nil?
      puts "Creating a family"
      family = Family.new(name: self.lastname)
      family.save
      end
      self.family = family
      save
      puts "Saved person instance is now: #

      {self.inspect}

      "
      puts "Finding self is now: #

      {Person.find(self.id).inspect}

      "
      end

      end

      class Family
      include Mongoid::Document
      include Mongoid::Timestamps
      field :name

      has_many :people, dependent: :destroy
      end

      person = Person.create(firstname: 'John', lastname: 'Smith')
      puts ""
      puts "after creation Person instance: #

      {person.inspect}

      "
      puts ""
      puts "finding that same instance: #

      {Person.find(person.id).inspect}

      "

                  1. output using mongoid 2.1.2
                    #Creating a family
                    #Saved person instance is now: #<Person _id: 4e36c7d79eb0710c97000001, _type: nil, created_at: 2011-08-01 15:35:51 UTC, updated_at: 2011-08-01 15:35:51 UTC, firstname: "John", lastname: "Smith", family_id: BSON::ObjectId('4e36c7d79eb0710c97000002')>
                    #Finding self is now: #<Person _id: 4e36c7d79eb0710c97000001, _type: nil, created_at: 2011-08-01 15:35:51 UTC, updated_at: 2011-08-01 15:35:51 UTC, firstname: "John", lastname: "Smith", family_id: nil>
                    #
                    #after creation Person instance: #<Person _id: 4e36c7d79eb0710c97000001, _type: nil, created_at: 2011-08-01 15:35:51 UTC, updated_at: 2011-08-01 15:35:51 UTC, firstname: "John", lastname: "Smith", family_id: BSON::ObjectId('4e36c7d79eb0710c97000002')>
                    #
                    #finding that same instance: #<Person _id: 4e36c7d79eb0710c97000001, _type: nil, created_at: 2011-08-01 15:35:51 UTC, updated_at: 2011-08-01 15:35:51 UTC, firstname: "John", lastname: "Smith", family_id: nil>
                  1. output using mongoid 2.0.2
                    #Creating a family
                    #Saved person instance is now: #<Person _id: 4e36c8089eb0710f4f000001, _type: nil, created_at: 2011-08-01 15:36:40 UTC, updated_at: 2011-08-01 15:36:41 UTC, firstname: "John", lastname: "Smith", family_id: BSON::ObjectId('4e36c8099eb0710f4f000002')>
                    #Finding self is now: #<Person _id: 4e36c8089eb0710f4f000001, _type: nil, created_at: 2011-08-01 15:36:40 UTC, updated_at: 2011-08-01 15:36:41 UTC, firstname: "John", lastname: "Smith", family_id: BSON::ObjectId('4e36c8099eb0710f4f000002')>
                    #
                    #after creation Person instance: #<Person _id: 4e36c8089eb0710f4f000001, _type: nil, created_at: 2011-08-01 15:36:40 UTC, updated_at: 2011-08-01 15:36:41 UTC, firstname: "John", lastname: "Smith", family_id: BSON::ObjectId('4e36c8099eb0710f4f000002')>
                    #
                    #finding that same instance: #<Person _id: 4e36c8089eb0710f4f000001, _type: nil, created_at: 2011-08-01 15:36:40 UTC, updated_at: 2011-08-01 15:36:41 UTC, firstname: "John", lastname: "Smith", family_id: BSON::ObjectId('4e36c8099eb0710f4f000002')>

      As you can see with mongoid 2.1.2 the family_id is nil when finding that person again (and is actually nil in the database). Another thing that is not demonstrated with above code is the fact that we create more complex relations than above and when relating more models AFTER the first one (in this case Family - which fails) the ids get set which is really strange.

            Assignee:
            Unassigned Unassigned
            Reporter:
            johnae John Axel Eriksson
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: