From 9aeba503c4bc321e4c5a14cd9c9fbf9fbcc60edb Mon Sep 17 00:00:00 2001 From: Ross Kaffenberger Date: Fri, 31 Mar 2017 18:11:15 -0400 Subject: Add default_scope note to AR guide [ci skip] Adds note to explain unexpected behavior described in issue #28561. Developers may be surprised that the internal representation of similar query conditions in Hash and Array formats in default_scope will yield different results for model initialization. Link: https://github.com/rails/rails/issues/28561 --- guides/source/active_record_querying.md | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'guides/source') diff --git a/guides/source/active_record_querying.md b/guides/source/active_record_querying.md index 2902c5d677..26d01d4ede 100644 --- a/guides/source/active_record_querying.md +++ b/guides/source/active_record_querying.md @@ -1381,8 +1381,9 @@ class Client < ApplicationRecord end ``` -NOTE: The `default_scope` is also applied while creating/building a record. -It is not applied while updating a record. E.g.: +NOTE: The `default_scope` is also applied while creating/building a record +when the scope arguments are given as a `Hash`. It is not applied while +updating a record. E.g.: ```ruby class Client < ApplicationRecord @@ -1393,6 +1394,17 @@ Client.new # => # Client.unscoped.new # => # ``` +Be aware that, when given in the `Array` format, `default_scope` query arguments +cannot be converted to a `Hash` for default attribute assignment. E.g.: + +```ruby +class Client < ApplicationRecord + default_scope { where("active = ?", true) } +end + +Client.new # => # +``` + ### Merging of scopes Just like `where` clauses scopes are merged using `AND` conditions. -- cgit v1.2.3