flyerhzm / bullet

help to kill N+1 queries and unused eager loading

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

False positive with count associtiaons

CarlosUvaSilva opened this issue · comments

I have this inside a controller

users = User.includes(
  :posts,
)

and this inside the User model

def number_of_posts
  if posts.loaded?
    posts.size
  else
    posts.count
  end
end

If I include posts I get the expected:
SELECT "posts".* FROM "posts" WHERE "posts"."user_id" IN ($1, $2, $3, $4, $5)

and if I don't include I get one of these for each user:
SELECT COUNT(*) FROM "posts" WHERE "posts"."user_id" = $1

Problem is Bullet is telling me to NOT include the posts

AVOID eager loading detected
  User => [:posts]
  Remove from your query: .includes([:posts])

Thank you!

I am also seeing massive false positives during upgrading Rails 6 to 7(bullet 7).

Same here

I think it supposed to be fixed in bullet 7.1.3