diff options
author | Gonçalo Silva <goncalossilva@gmail.com> | 2011-05-08 03:54:55 +0100 |
---|---|---|
committer | Gonçalo Silva <goncalossilva@gmail.com> | 2011-05-08 03:54:55 +0100 |
commit | aec7456f81985b88d6d604679d754636183b5b3a (patch) | |
tree | 798fcab8ae0c6e2c8c5a63e1f39308ff4bb5a20c /railties/guides/source/active_record_querying.textile | |
parent | 1c2b2233c3a7ec76c0a0eddf5b8be45c489be133 (diff) | |
parent | 70f9558d0e4b8e605576693cbb489caa5cf6a2bc (diff) | |
download | rails-aec7456f81985b88d6d604679d754636183b5b3a.tar.gz rails-aec7456f81985b88d6d604679d754636183b5b3a.tar.bz2 rails-aec7456f81985b88d6d604679d754636183b5b3a.zip |
Merge branch 'master' of https://github.com/rails/rails into performance_test
Conflicts:
activesupport/lib/active_support/testing/performance.rb
Diffstat (limited to 'railties/guides/source/active_record_querying.textile')
-rw-r--r-- | railties/guides/source/active_record_querying.textile | 43 |
1 files changed, 35 insertions, 8 deletions
diff --git a/railties/guides/source/active_record_querying.textile b/railties/guides/source/active_record_querying.textile index df8e35ed33..579a323d57 100644 --- a/railties/guides/source/active_record_querying.textile +++ b/railties/guides/source/active_record_querying.textile @@ -56,6 +56,7 @@ The methods are: * +select+ * +group+ * +order+ +* +reorder+ * +limit+ * +offset+ * +joins+ @@ -418,7 +419,7 @@ SELECT viewable_by, locked FROM clients Be careful because this also means you're initializing a model object with only the fields that you've selected. If you attempt to access a field that is not in the initialized record you'll receive: <shell> -ActiveRecord::MissingAttributeError: missing attribute: <attribute> +ActiveModel::MissingAttributeError: missing attribute: <attribute> </shell> Where +<attribute>+ is the attribute you asked for. The +id+ method will not raise the +ActiveRecord::MissingAttributeError+, so just be careful when working with associations because they need the +id+ method to function properly. @@ -495,9 +496,9 @@ This will return single order objects for each day, but only for the last month. h3. Overriding Conditions -You can specify certain conditions to be excepted by using the +except+ method. +h4. +except+ -For example: +You can specify certain conditions to be excepted by using the +except+ method. For example: <ruby> Post.where('id > 10').limit(20).order('id asc').except(:order) @@ -509,9 +510,9 @@ The SQL that would be executed: SELECT * FROM posts WHERE id > 10 LIMIT 20 </sql> -You can also override conditions using the +only+ method. +h4. +only+ -For example: +You can also override conditions using the +only+ method. For example: <ruby> Post.where('id > 10').limit(20).order('id desc').only(:order, :where) @@ -523,6 +524,32 @@ The SQL that would be executed: SELECT * FROM posts WHERE id > 10 ORDER BY id DESC </sql> +h4. +reorder+ + +The +reorder+ method overrides the default scope order. For example: + +<ruby> +class Post < ActiveRecord::Base + .. + .. + has_many :comments, :order => 'posted_at DESC' +end + +Post.find(10).comments.reorder('name') +</ruby> + +The SQL that would be executed: + +<sql> +SELECT * FROM posts WHERE id = 10 ORDER BY name +</sql> + +In case the +reorder+ clause is not used, the SQL executed would be: + +<sql> +SELECT * FROM posts WHERE id = 10 ORDER BY posted_at DESC +</sql> + h3. Readonly Objects Active Record provides +readonly+ method on a relation to explicitly disallow modification or deletion of any of the returned object. Any attempt to alter or destroy a readonly record will not succeed, raising an +ActiveRecord::ReadOnlyRecord+ exception. @@ -546,7 +573,7 @@ Active Record provides two locking mechanisms: h4. Optimistic Locking -Optimistic locking allows multiple users to access the same record for edits, and assumes a minimum of conflicts with the data. It does this by checking whether another process has made changes to a record since it was opened. An +ActiveRecord::StaleObjectError+ exception is thrown if that has occurred and the update is ignored. +Optimistic locking allows multiple users to access the same record for edits, and assumes a minimum of conflicts with the data. It does this by checking whether another process has made changes to a record since it was opened. An +ActiveRecord::StaleObjectError+ exception is thrown if that has occurred and the update is ignored. <strong>Optimistic locking column</strong> @@ -993,7 +1020,7 @@ Client.count # SELECT count(*) AS count_all FROM clients </ruby> -Or on a relation : +Or on a relation: <ruby> Client.where(:first_name => 'Ryan').count @@ -1060,7 +1087,7 @@ If you want to find the sum of a field for all records in your table you can cal Client.sum("orders_count") </ruby> -For options, please see the parent section, "Calculations":#calculations. +For options, please see the parent section, "Calculations":#calculations. h3. Changelog |