Browse files

Merge branch 'master' of

  • Loading branch information...
2 parents 272e70e + eeb8d1c commit 9b109c9e665da043d9b77f065749db6a51001b9e @toretore toretore committed Oct 9, 2008
Showing with 51 additions and 13 deletions.
  1. +47 −13 railties/doc/guides/activerecord/finders.txt
  2. +4 −0 railties/doc/guides/routing/routing_outside_in.txt
60 railties/doc/guides/activerecord/finders.txt
@@ -201,19 +201,17 @@
== Lock
-== Making It All Work Together
-You can chain these options together in no particular order as ActiveRecord will write the correct SQL for you. For example you could do this:
+If you're wanting to stop race conditions for a specific record, say for example you're incrementing a single field for a record you can use the lock option to ensure that the record is updated correctly. It's recommended this be used inside a transaction.
-[source, ruby]
-Client.find(:all, :order => "created_at DESC", :select => "viewable_by, created_at", :conditions => ["viewable_by = ?", params[:level]], :limit => 10, :include => :orders, :joins => :address)
+[source, Ruby]
+Topic.transaction do
+ t = Topic.find(params[:id], :lock => true)
+ t.increment!(:views)
-Which should execute a query like
+== Making It All Work Together
-[source, sql]
-example goes here
+You can chain these options together in no particular order as ActiveRecord will write the correct SQL for you. If you specify two instances of the same options inside the find statement ActiveRecord will use the latter.
== Eager Loading
@@ -262,7 +260,12 @@ will either assign an existing client object with the name 'Ryan' to the client
== Finding By SQL
+If you'd like to use your own SQL to find records a table you can use `find_by_sql`. `find_by_sql` will return an array of objects even if it only returns a single record in it's call to the database. For example you could run this query:
+[source, ruby]
+Client.find_by_sql("SELECT * FROM clients INNER JOIN orders ON = orders.client_id ORDER clients.created_at desc")
+`find_by_sql` provides you with a simple way of making custom calls to the database and converting those to objects.
== Working with Associations
@@ -315,11 +318,38 @@ Remember that named scopes are stackable, so you will be able to do `Client.rece
== Existance of Objects
+If you simply want to check for the existance of the object there's a method called `exists?`. This method will query the database using the same query as find, but instead of returning an object or collection of objects it will return either true or false.
+[source, ruby]
+The above code will check for the existance of a clients table record with the id of 1 and return true if it exists.
+[source, ruby]
+# or
+`exists?` also takes multiple ids, as shown by the above code, but the catch is that it will return true if any one of those records exists.
+Further more, `exists` takes a `conditions` option much like find:
+[source, ruby]
+Client.exists?(:conditions => "first_name = 'Ryan'")
== Calculations
+=== Count
+If you want to see how many records are in your models table you could call `Client.count` and that will return the number. If you want to be more specific and find all the clients with their age present in the database you can use `Client.count(:age)`.
+`count` takes conditions much in the same way `exists?` does:
+[source, ruby]
+Client.count(:conditions => "first_name = 'Ryan'")
+[source, sql]
+SELECT count(*) AS count_all FROM `clients` WHERE (first_name = 1)
== With Scope
@@ -353,3 +383,7 @@ Thanks to Mike Gunderloy for his tips on creating this guide.
=== Monday, 06 October 2008
1. Added section in Eager Loading about using conditions on tables that are not the model's own.
+=== Thursday, 09 October 2008
+1. Wrote section about lock option and tidied up "Making it all work together" section.
+2. Added section on using count.
4 railties/doc/guides/routing/routing_outside_in.txt
@@ -384,6 +384,8 @@ Routes recognized by this entry would include:
NOTE: In most cases, it's simpler to recognize URLs of this sort by creating nested resources, as discussed in the next section.
+NOTE: You can also use +:path_prefix+ with non-RESTful routes.
==== Using :name_prefix
You can use the :name_prefix option to avoid collisions between routes. This is most useful when you have two resources with the same name that use +:path_prefix+ to map differently. For example:
@@ -396,6 +398,8 @@ map.resources :photos, :path_prefix => '/agencies/:agency_id', :name_prefix => '
This combination will give you route helpers such as +photographer_photos_path+ and +agency_edit_photo_path+ to use in your code.
+NOTE: You can also use +:name_prefix+ with non-RESTful routes.
=== Nested Resources
It's common to have resources that are logically children of other resources. For example, suppose your application includes these models:

0 comments on commit 9b109c9

Please sign in to comment.