aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--guides/source/active_record_querying.textile24
1 files changed, 19 insertions, 5 deletions
diff --git a/guides/source/active_record_querying.textile b/guides/source/active_record_querying.textile
index 9f18a8c90d..a02ef21b00 100644
--- a/guides/source/active_record_querying.textile
+++ b/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.