-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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
Explained difference between scope & class method #33653
Conversation
Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @schneems (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review. Please see the contribution instructions for more information. |
Example for false information being spread based on this guide: https://stackoverflow.com/questions/32930312/ruby-on-rails-activerecord-scopes-vs-class-methods |
Thanks! |
@@ -1277,14 +1277,20 @@ class Article < ApplicationRecord | |||
end | |||
``` | |||
|
|||
This is exactly the same as defining a class method, and which you use is a matter of personal preference: | |||
This is almost the same as defining a class method, except for that scopes always return an `ActiveRecord::Relation` object. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
exactly the same is not true, but the remaining part is almost the same with here:
rails/guides/source/active_record_querying.md
Lines 1360 to 1370 in 97feb49
Like the other examples, this will behave similarly to a class method. | |
```ruby | |
class Article < ApplicationRecord | |
def self.created_before(time) | |
where("created_at < ?", time) if time.present? | |
end | |
end | |
``` | |
However, there is one important caveat: A scope will always return an `ActiveRecord::Relation` object, even if the conditional evaluates to `false`, whereas a class method, will return `nil`. This can cause `NoMethodError` when chaining class methods with conditionals, if any of the conditionals return `false`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about removing this part in favor of the info below in order to not repeat yourself?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it is a good idea to me.
The sentence "This is exactly the same as defining a class method ..." is not true, so rails#33653 fixed it, but added changes repeat what is explained a few lines below. We can remove this part since a user is able to get info about the difference between scopes and class methods below. Context rails#33653 (comment). Reverts rails#33653, 97feb49.
Summary
Today in the Ruby on Rails slack, the question arose what the difference between a scope and a class method is.
After googling I found an accepted stack overflow answer which I partly copied in order to answer the question.
This part copy of mine included the wrong quote from this guide, that a scope and a class method is exactly the same. This is simply not true.