Skip to content

Commit 2a0a264

Browse files
committed
Merge pull request rails#20763 from maurogeorge/default_scope_create-doc
Add a note about default_scope and create records [ci skip]
2 parents 68af636 + 2ef1de0 commit 2a0a264

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

guides/source/active_record_querying.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,6 +1266,18 @@ class Client < ActiveRecord::Base
12661266
end
12671267
```
12681268

1269+
NOTE: The `default_scope` is also applied while creating/building a record.
1270+
It is not applied while updating a record. E.g.:
1271+
1272+
```ruby
1273+
class Client < ActiveRecord::Base
1274+
default_scope { where(active: true) }
1275+
end
1276+
1277+
Client.new # => #<Client id: nil, active: true>
1278+
Client.unscoped.new # => #<Client id: nil, active: nil>
1279+
```
1280+
12691281
### Merging of scopes
12701282

12711283
Just like `where` clauses scopes are merged using `AND` conditions.

0 commit comments

Comments
 (0)