1

I understand that generally it is better to avoid having default scopes in Rails models, however, as some associations are always needed & instead of having the .includes method in the controller every time for the model, I prefer having that includes in a default scope in the model itself.

Is there any major drawback of this change (moving .includes to model's default_scope instead of controller)?

So for example:

Instead of having in the controller:

Brand.includes(:car_model)

I want to have includes in the Brand model itself this:

default_scope { includes(:car_model) }

Note:

From what I see is that bullet is now saying avoid eager load to avoid this includes instead of the use eager load that I had before on the same model & included association.

Ahmed Elkoussy
  • 8,162
  • 10
  • 60
  • 85
  • I don't want to explain why. Just don't do that! – chumakoff Oct 10 '19 at 12:32
  • I will consider this as a joke, because for me, I do things based on good reasons only – Ahmed Elkoussy Oct 10 '19 at 12:43
  • I think because you only want to includes `car_model` on queries that will then access the relationship. I think Bullet, has found an instance where that isn't happening and therefore you should remove the default scope from the model and only use it on queries where it's needed. IMO, the problem with a default scope is just this. Instead of only using it where needed, you now have to find where it's not and using the `unscoped` helper on it to remove the default scope – Int'l Man Of Coding Mystery Nov 15 '19 at 09:53

0 Answers0