diff options
author | Yves Senn <yves.senn@gmail.com> | 2014-04-03 10:31:29 +0200 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2014-04-03 10:31:29 +0200 |
commit | c0e0e80fccf350ae5b59c9b17ec45577ff60f3cb (patch) | |
tree | 4d183cf634e15b4c65189330fa87e79e73ad82f3 /guides | |
parent | 79f06a91345a301e1a2f2378552b5860a493b256 (diff) | |
parent | 34a3d425ea78786c63757028257b63b895e60960 (diff) | |
download | rails-c0e0e80fccf350ae5b59c9b17ec45577ff60f3cb.tar.gz rails-c0e0e80fccf350ae5b59c9b17ec45577ff60f3cb.tar.bz2 rails-c0e0e80fccf350ae5b59c9b17ec45577ff60f3cb.zip |
Merge pull request #14576 from ariabov/scope_sections_reordering
Change order of scope subsections in Active Record Querying guide [ci skip]
Diffstat (limited to 'guides')
-rw-r--r-- | guides/CHANGELOG.md | 4 | ||||
-rw-r--r-- | guides/source/active_record_querying.md | 59 |
2 files changed, 33 insertions, 30 deletions
diff --git a/guides/CHANGELOG.md b/guides/CHANGELOG.md index a7c215c295..246e1d3b96 100644 --- a/guides/CHANGELOG.md +++ b/guides/CHANGELOG.md @@ -1 +1,5 @@ +* Switched the order of `Applying a default scope` and `Merging of scopes` subsections so default scopes are introduced first. + + *Alex Riabov* + Please check [4-1-stable](https://github.com/rails/rails/blob/4-1-stable/guides/CHANGELOG.md) for previous changes. diff --git a/guides/source/active_record_querying.md b/guides/source/active_record_querying.md index 0a332d7dd9..2a76df156c 100644 --- a/guides/source/active_record_querying.md +++ b/guides/source/active_record_querying.md @@ -1231,6 +1231,35 @@ Using a class method is the preferred way to accept arguments for scopes. These category.posts.created_before(time) ``` +### Applying a default scope + +If we wish for a scope to be applied across all queries to the model we can use the +`default_scope` method within the model itself. + +```ruby +class Client < ActiveRecord::Base + default_scope { where("removed_at IS NULL") } +end +``` + +When queries are executed on this model, the SQL query will now look something like +this: + +```sql +SELECT * FROM clients WHERE removed_at IS NULL +``` + +If you need to do more complex things with a default scope, you can alternatively +define it as a class method: + +```ruby +class Client < ActiveRecord::Base + def self.default_scope + # Should return an ActiveRecord::Relation. + end +end +``` + ### Merging of scopes Just like `where` clauses scopes are merged using `AND` conditions. @@ -1284,36 +1313,6 @@ User.where(state: 'inactive') As you can see above the `default_scope` is being merged in both `scope` and `where` conditions. - -### Applying a default scope - -If we wish for a scope to be applied across all queries to the model we can use the -`default_scope` method within the model itself. - -```ruby -class Client < ActiveRecord::Base - default_scope { where("removed_at IS NULL") } -end -``` - -When queries are executed on this model, the SQL query will now look something like -this: - -```sql -SELECT * FROM clients WHERE removed_at IS NULL -``` - -If you need to do more complex things with a default scope, you can alternatively -define it as a class method: - -```ruby -class Client < ActiveRecord::Base - def self.default_scope - # Should return an ActiveRecord::Relation. - end -end -``` - ### Removing All Scoping If we wish to remove scoping for any reason we can use the `unscoped` method. This is |