Permalink
Browse files

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

…skip]
  • Loading branch information...
Francesco Rodriguez
Francesco Rodriguez committed Sep 8, 2012
1 parent 065fcfb commit 56652a6bf589e3f92f7faaa2fec89ef70d740d1b
Showing with 19 additions and 5 deletions.
  1. +19 −5 guides/source/active_record_querying.textile
@@ -1144,40 +1144,54 @@ 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
default_scope { where("removed_at IS NULL") }
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
</ruby>
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.

0 comments on commit 56652a6

Please sign in to comment.