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

Belongs to will not return association

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

      I have no idea why mongoid started to acting strange. This is the second issue I am filing.

      I have a model:

      class SpannedLineItem
          belongs_to :master_line_item, class_name => 'LineItem'
      end
      
      class LineItem
        belongs_to :spanned, :class_name => 'SpannedLineItem'
      end
      

      My code in the controller looks like this:

      @line_item = LineItem.find(params[:id])
      
      if(@line_item.spanned)
         @line_item = @line_item.spanned.master_line_item
      end
      

      Basically if the line item is spanned I am interested in another - master - line item.

      The problem is, @line_item in the end will equal the non master item...
      I checked the database, mongoid does look up the fields, but does not continue to look for the line item pointed by master_line_item_id
      MONGODB finance_rails2_development['line_items'].find({:_id=>BSON::ObjectId('4e57f61bb9fdd9121b000600')})
      MONGODB finance_rails2_development['spanned_line_items'].find({:_id=>BSON::ObjectId('4e57f61bb9fdd9121b0005fe')})

      Modifying the controller to this:
      if(@line_item.spanned)
      @line_item = LineItem.find(@line_item.spanned.master_line_item_id)
      end

      Does work and the log shows three finds:
      MONGODB finance_rails2_development['line_items'].find({:_id=>BSON::ObjectId('4e57f61bb9fdd9121b000600')})
      MONGODB finance_rails2_development['spanned_line_items'].find({:_id=>BSON::ObjectId('4e57f61bb9fdd9121b0005fe')})
      MONGODB finance_rails2_development['line_items'].find({:_id=>BSON::ObjectId('4e57f61bb9fdd9121b0005fd')})

      I am using 2.1.9

            Assignee:
            Unassigned Unassigned
            Reporter:
            mb-dev mb-dev
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: