magnusvk/counter_culture

The initialized count is incorrect when used with the where clause

shuuuuun opened this issue · 2 comments

When I use a counter column in a where clause before initialize, the count that is set seems to be incorrect.

If I add the following test case to spec/counter_culture_spec.rb, it fails.

# spec/counter_culture_spec.rb

  it "should initialize counts correctly when using with where clause" do
    user = User.new
    expect(user.reviews_count).to eq(0) # => success

    user = User.where(reviews_count: 2).new
    expect(user.reviews_count).to eq(0) # => failure, expected: 0, got: 2
  end

(In my actual project, I used the first_or_initialize method instead of new.)

gem 'counter_culture', '2.8'

Thank you.

So—I don’t think this has anything to do with counter_culture. You’re telling Rails to initialize reviews_count with the value 2, so it does. If that’s incorrect you should just stop doing that by dropping the where.

Oh, I see...
I misunderstood the specification of the where clause in Rails.
Sorry for the wrong report! 🙇