-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Avoiding ActiveRecord default_scope
#266
Comments
default_scope { where(deleted_at: nil) } Most of the time you want only non-deleted records, so this is valid. I reject the argument that it's better to spray But otherwise I think it is problematic or "evil" as we are so accustomed to saying. I think the thing to recommend is an explicit scope. |
Thanks for input! Yes, that is the one use case. With every tool we get conveniences and some drawbacks alongside. Question would be is it worth it? Should we compare these drawbacks and come to a conclusion? For soft deleting itself I am mostly against for user data, but that's whole another topic and use case is not only limited to that. Perhaps there are better solutions? I won't try to repeat what's already been documented quiet nicely so attaching an article here: Curious about more opinions 🙏 |
There's a PR open to add this to rubocop-rails: rubocop/rubocop-rails#267 |
I also feel like there's a certain pressure when using default_scope {
where(archived: false)
} It's sane for me to define the default scope to return non-archived values rather than explicitly saying I need non-archived only everywhere. |
Thank you! I would've gone ahead and open a PR in this project for the style guide but I see that the description for |
@gadimbaylisahil Please, go ahead and create a PR. I will update my linked PR with your examples and link to the style guide then 😄 |
Right on |
Done, I kept your examples and added 2 more references in #267. Thanks for the work! |
Two problems with
Otherwise, I don't really see it as being problematic. The example with soft-deletions @flanger001 mentions is pretty legit, and it's not about soft-deletions only, it's also for some default
So, even though there are those two culprits, the benefits of properly using
|
Closing as stale. Thank you all for participating. |
Hi, Rubocop community!
I have myself used
default_scope
in Active Record and have always regretted it. In general the communityopinion on
default_scope
is overall negative (correct me if I may be wrong, please).Main problem is its implicitness in associations for me. Let's say we have:
Googling default_scope already returns quiet a few results:
link1
link2
link3
It being usually problematic, how about to recommend to avoid it in style guides?
Associated PR: #267
Associated PR in
rubocop-hq/rubocop-rails
: rubocop/rubocop-rails#267The text was updated successfully, but these errors were encountered: