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

Count not working on HABTM relationships

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

      This fails:

       ruby
      class Account
        include Mongoid::Document
        
        field :following_ids, type: Array, default: []
        field :follower_ids,  type: Array, default: []
        
        has_and_belongs_to_many :followers, :class_name => 'Account', :foreign_key => 'follower_ids'
        has_and_belongs_to_many :followees, :class_name => 'Account', :foreign_key => 'following_ids'
      end
      
       ruby
      require 'test_helper'
      
      class AccountTest < ActiveSupport::TestCase
        setup do
          @account = FactoryGirl.create(:account)
          @other_account = FactoryGirl.create(:account)
        end
      
        test "if count works" do
          assert_difference "@account.followees.count" do
            @account.followees << @other_account
          end
        end
      
        test "if same thing with length works" do
          assert_difference "@account.followees.length" do
            @account.followees << @other_account
          end
        end
      end
      

      Result:

      AccountTest:
           FAIL if count works (0.01s) 
                "@account.followees.count" didn't change by 1.
      <1> expected but was
      <0>.
                /Users/marc/.rvm/gems/ruby-1.9.2-p180/bundler/gems/rails-e59491355e92/activesupport/lib/active_support/testing/assertions.rb:47:in `block in assert_difference'
           PASS if same thing with length works (0.00s)
      

            Assignee:
            Unassigned Unassigned
            Reporter:
            marclove marclove
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: