Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #14576 from ariabov/scope_sections_reordering
Change order of scope subsections in Active Record Querying guide [ci skip]
  • Loading branch information
senny committed Apr 3, 2014
2 parents 79f06a9 + 34a3d42 commit c0e0e80
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 30 deletions.
4 changes: 4 additions & 0 deletions 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. Please check [4-1-stable](https://github.com/rails/rails/blob/4-1-stable/guides/CHANGELOG.md) for previous changes.
59 changes: 29 additions & 30 deletions guides/source/active_record_querying.md
Expand Up @@ -1231,6 +1231,35 @@ Using a class method is the preferred way to accept arguments for scopes. These
category.posts.created_before(time) 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 ### Merging of scopes


Just like `where` clauses scopes are merged using `AND` conditions. Just like `where` clauses scopes are merged using `AND` conditions.
Expand Down Expand Up @@ -1284,36 +1313,6 @@ User.where(state: 'inactive')
As you can see above the `default_scope` is being merged in both As you can see above the `default_scope` is being merged in both
`scope` and `where` conditions. `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 ### Removing All Scoping


If we wish to remove scoping for any reason we can use the `unscoped` method. This is If we wish to remove scoping for any reason we can use the `unscoped` method. This is
Expand Down

0 comments on commit c0e0e80

Please sign in to comment.