-
-
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
Faster Rails: Use exists
instead of present?
to check if a Record Exists
#232
Comments
Completely agree! Wanna open a PR? |
Ya sure! Can you guide me where I've to make the changes? |
Here's another one from a performance blog post
|
@zoras I think the ActiveRecord Queries section of the README would be a good place to add a bullet point about (The Rubocop repo is more about the tool that can detect rule violations and enforce some of the rules, and the Ruby Style Guide is specific to Ruby whereas the Rails Style Guide includes the ActiveRecord framework.) |
Quickly summarizing the methods. Check for one or more records:
Check for no records:
Misc:
Note: The methods above are assumed without block. When providing a block to |
Somewhat unrelated note: it is unfortunate that the method is named |
@3limin4t0r Would you like to send a PR? |
@pirj I wouldn't know what to really say in a "style guide" about this. I feel like this is not as much about style, since one method is not clearly better then the other. This is more about selecting the right tool for the job. Knowing what tools you have available to you is always a good practice. Although now I think about it, there is a case to be made to recommend I might try to write a PR, and see if I can give some well formulated general advise. |
Sounds about right, thank you. Looking forward to it. |
Prefer
exists?
instead ofpresent?
for checking record according to this blog post.Also, prefer
any?
andempty?
when checking for the existence of an association record without any scope.The text was updated successfully, but these errors were encountered: