Skip to content
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

Merged
merged 1 commit into from Aug 19, 2018
Merged

Explained difference between scope & class method #33653

merged 1 commit into from Aug 19, 2018

Conversation

ghost
Copy link

@ghost ghost commented Aug 19, 2018

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.

@rails-bot
Copy link

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.

@ghost
Copy link
Author

ghost commented Aug 19, 2018

Example for false information being spread based on this guide: https://stackoverflow.com/questions/32930312/ruby-on-rails-activerecord-scopes-vs-class-methods

@schneems
Copy link
Member

Thanks!

@schneems schneems merged commit 8788005 into rails:master Aug 19, 2018
@@ -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.
Copy link
Member

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:

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`.

Copy link
Contributor

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?

Copy link
Member

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.

bogdanvlviv added a commit to bogdanvlviv/rails that referenced this pull request Aug 19, 2018
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants