Merge pull request #33655 from bogdanvlviv/follow-up-33653

DRY in Active Record Query Interface [ci skip]
This commit is contained in:
Ryuta Kamizono 2018-08-20 01:37:34 +09:00 committed by GitHub
commit fd1ec91c73
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1277,22 +1277,6 @@ class Article < ApplicationRecord
end
```
This is almost the same as defining a class method, except for that scopes always return an `ActiveRecord::Relation` object.
```ruby
class Article < ApplicationRecord
scope :by_user, ->(user) { where(user: user) if user }
def self.published_since(date)
where('published_at > ?', date) if date
end
end
Article.by_user(nil)
# => #<ActiveRecord::Relation []>
Article.published_since(nil)
# => nil
```
Scopes are also chainable within scopes:
```ruby