aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/associations/deprecated_counter_cache_on_has_many_through_test.rb
blob: 48f7ddbe83246b5c4fcde4f24022989e6d96aa99 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
require "cases/helper"

class DeprecatedCounterCacheOnHasManyThroughTest < ActiveRecord::TestCase
  class Post < ActiveRecord::Base
    has_many :taggings, as: :taggable
    has_many :tags, through: :taggings
  end

  class Tagging < ActiveRecord::Base
    belongs_to :taggable, polymorphic: true
    belongs_to :tag
  end

  class Tag < ActiveRecord::Base
  end

  test "counter caches are updated in the database if the belongs_to association doesn't specify a counter cache" do
    post = Post.create!(title: 'Hello', body: 'World!')
    assert_deprecated { post.tags << Tag.create!(name: 'whatever') }

    assert_equal 1, post.tags.size
    assert_equal 1, post.tags_count
    assert_equal 1, post.reload.tags.size
    assert_equal 1, post.reload.tags_count
  end
end