diff options
author | Francesco Rodriguez <lrodriguezsanc@gmail.com> | 2012-09-08 13:10:40 -0500 |
---|---|---|
committer | Francesco Rodriguez <lrodriguezsanc@gmail.com> | 2012-09-08 13:10:40 -0500 |
commit | 56652a6bf589e3f92f7faaa2fec89ef70d740d1b (patch) | |
tree | e01ffe4209def0b3f895dd01cb5318c5d3dde988 /guides/source | |
parent | 065fcfbfa156d4cca0c87ddf3d4d0a5d48eccbe7 (diff) | |
download | rails-56652a6bf589e3f92f7faaa2fec89ef70d740d1b.tar.gz rails-56652a6bf589e3f92f7faaa2fec89ef70d740d1b.tar.bz2 rails-56652a6bf589e3f92f7faaa2fec89ef70d740d1b.zip |
add note about using block form of unscoped in AR Querying guide [ci skip]
Diffstat (limited to 'guides/source')
-rw-r--r-- | guides/source/active_record_querying.textile | 24 |
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. |