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

counter_cache should not have to reload

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

      counter_cache should also update the counter in memory.

      This spec should pass:

      diff --git a/spec/mongoid/relations/referenced/many_spec.rb b/spec/mongoid/relations/referenced/many_spec.rb
      index 8c1f098..cb9e830 100644
      --- a/spec/mongoid/relations/referenced/many_spec.rb
      +++ b/spec/mongoid/relations/referenced/many_spec.rb
      @@ -173,7 +173,19 @@ describe Mongoid::Relations::Referenced::Many do
                 end
      
                 it "increments the counter cache" do
      -            expect(person.reload.posts_count).to eq(1)
      +            expect(person[:posts_count]).to eq(1)
      +          end
      +
      +          context "when saving another post" do
      +
      +            before do
      +              person.reload
      +              person.posts.send(method, Post.new)
      +            end
      +
      +            it "increments the counter cache" do
      +              expect(person.posts_count).to eq(2)
      +            end
                 end
      
                 context "when documents already exist on the relation" do
      

            Assignee:
            arthurnn Arthur Nogueira Neves
            Reporter:
            arthurnn Arthur Nogueira Neves
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: