diff options
Diffstat (limited to 'guides/source/active_record_querying.textile')
-rw-r--r-- | guides/source/active_record_querying.textile | 38 |
1 files changed, 31 insertions, 7 deletions
diff --git a/guides/source/active_record_querying.textile b/guides/source/active_record_querying.textile index 80c9260a0d..96ae5b2972 100644 --- a/guides/source/active_record_querying.textile +++ b/guides/source/active_record_querying.textile @@ -266,7 +266,7 @@ SELECT * FROM clients WHERE (clients.id IN (1,10)) WARNING: <tt>Model.find(array_of_primary_key)</tt> will raise an +ActiveRecord::RecordNotFound+ exception unless a matching record is found for <strong>all</strong> of the supplied primary keys. -h5. take +h5(#take-n-objects). take <tt>Model.take(limit)</tt> retrieves the first number of records specified by +limit+ without any explicit ordering: @@ -282,7 +282,7 @@ The SQL equivalent of the above is: SELECT * FROM clients LIMIT 2 </sql> -h5. first +h5(#first-n-objects). first <tt>Model.first(limit)</tt> finds the first number of records specified by +limit+ ordered by primary key: @@ -298,7 +298,7 @@ The SQL equivalent of the above is: SELECT * FROM clients LIMIT 2 </sql> -h5. last +h5(#last-n-objects). last <tt>Model.last(limit)</tt> finds the number of records specified by +limit+ ordered by primary key in descending order: @@ -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,15 +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: + +<ruby> +class Client < ActiveRecord::Base + def self.default_scope + # 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 @@ -1168,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 these 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. @@ -1218,7 +1242,7 @@ Client.find_or_create_by_first_name(:first_name => "Andy", :locked => false) This method still works, but it's encouraged to use +first_or_create+ because it's more explicit on which arguments are used to _find_ the record and which are used to _create_, resulting in less confusion overall. -h4. +first_or_create!+ +h4(#first_or_create_bang). +first_or_create!+ You can also use +first_or_create!+ to raise an exception if the new record is invalid. Validations are not covered on this guide, but let's assume for a moment that you temporarily add |