Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add note about using block form of unscoped in AR Querying guide [ci …

…skip]
  • Loading branch information...
commit 56652a6bf589e3f92f7faaa2fec89ef70d740d1b 1 parent 065fcfb
@frodsan frodsan authored
Showing with 19 additions and 5 deletions.
  1. +19 −5 guides/source/active_record_querying.textile
View
24 guides/source/active_record_querying.textile
@@ -1144,7 +1144,8 @@ category.posts.created_before(time)
h4. 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.
+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
@@ -1152,25 +1153,29 @@ class Client < ActiveRecord::Base
end
</ruby>
-When queries are executed on this model, the SQL query will now look something like this:
+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
</sql>
-If you need to do more complex things with a default scope, you can alternatively define it as a class method:
+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
+ # Should return an ActiveRecord::Relation.
end
end
</ruby>
h4. Removing all scoping
-If we wish to remove scoping for any reason we can use the +unscoped+ method. This is especially useful if a +default_scope+ is specified in the model and should not be applied for this particular query.
+If we wish to remove scoping for any reason we can use the +unscoped+ method. This is
+especially useful if a +default_scope+ is specified in the model and should not be
+applied for this particular query.
<ruby>
Client.unscoped.all
@@ -1178,6 +1183,15 @@ Client.unscoped.all
This method removes all scoping and will do a normal query on the table.
+Note that chaining +unscoped+ with a +scope+ does not work. In this cases, it is
+recommended that you use the block form of +unscoped+:
+
+<ruby>
+Client.unscoped {
+ Client.created_before(Time.zome.now)
+}
+</ruby>
+
h3. Dynamic Finders
For every field (also known as an attribute) you define in your table, Active Record provides a finder method. If you have a field called +first_name+ on your +Client+ model for example, you get +find_by_first_name+ and +find_all_by_first_name+ for free from Active Record. If you have a +locked+ field on the +Client+ model, you also get +find_by_locked+ and +find_all_by_locked+ methods.
Please sign in to comment.
Something went wrong with that request. Please try again.