diff options
Diffstat (limited to 'railties/guides')
-rw-r--r-- | railties/guides/source/active_record_querying.textile | 26 |
1 files changed, 13 insertions, 13 deletions
diff --git a/railties/guides/source/active_record_querying.textile b/railties/guides/source/active_record_querying.textile index 6b6f4d4983..81d73c4ccc 100644 --- a/railties/guides/source/active_record_querying.textile +++ b/railties/guides/source/active_record_querying.textile @@ -87,7 +87,7 @@ Using <tt>Model.find(primary_key)</tt>, you can retrieve the object correspondin <ruby> # Find the client with primary key (id) 10. client = Client.find(10) -=> #<Client id: 10, first_name: "Ryan"> +# => #<Client id: 10, first_name: "Ryan"> </ruby> The SQL equivalent of the above is: @@ -104,7 +104,7 @@ h5. +first+ <ruby> client = Client.first -=> #<Client id: 1, first_name: "Lifo"> +# => #<Client id: 1, first_name: "Lifo"> </ruby> The SQL equivalent of the above is: @@ -121,7 +121,7 @@ h5. +last+ <ruby> client = Client.last -=> #<Client id: 221, first_name: "Russel"> +# => #<Client id: 221, first_name: "Russel"> </ruby> The SQL equivalent of the above is: @@ -138,7 +138,7 @@ h5(#first_1). +first!+ <ruby> client = Client.first! -=> #<Client id: 1, first_name: "Lifo"> +# => #<Client id: 1, first_name: "Lifo"> </ruby> The SQL equivalent of the above is: @@ -155,7 +155,7 @@ h5(#last_1). +last!+ <ruby> client = Client.last! -=> #<Client id: 221, first_name: "Russel"> +# => #<Client id: 221, first_name: "Russel"> </ruby> The SQL equivalent of the above is: @@ -175,7 +175,7 @@ h5. Using Multiple Primary Keys <ruby> # Find the clients with primary keys 1 and 10. client = Client.find([1, 10]) # Or even Client.find(1, 10) -=> [#<Client id: 1, first_name: "Lifo">, #<Client id: 10, first_name: "Ryan">] +# => [#<Client id: 1, first_name: "Lifo">, #<Client id: 10, first_name: "Ryan">] </ruby> The SQL equivalent of the above is: @@ -252,7 +252,7 @@ Invoice.find_in_batches(:include => :invoice_lines) do |invoices| end </ruby> -The above will each time yield to the supplied block an arrays of 1000 invoices (or the remaining invoices, if less than 1000). +The above will each time yield to the supplied block an array of 1000 invoices (or the remaining invoices, if less than 1000). NOTE: The +:include+ option allows you to name associations that should be loaded alongside with the models. @@ -1032,7 +1032,7 @@ Suppose you want to find a client named 'Andy', and if there's none, create one <ruby> Client.where(:first_name => 'Andy').first_or_create(:locked => false) -=> #<Client id: 1, first_name: "Andy", orders_count: 0, locked: false, created_at: "2011-08-30 06:09:27", updated_at: "2011-08-30 06:09:27"> +# => #<Client id: 1, first_name: "Andy", orders_count: 0, locked: false, created_at: "2011-08-30 06:09:27", updated_at: "2011-08-30 06:09:27"> </ruby> The SQL generated by this method looks like this: @@ -1070,7 +1070,7 @@ to your +Client+ model. If you try to create a new +Client+ without passing an + <ruby> Client.where(:first_name => 'Andy').first_or_create!(:locked => false) -=> ActiveRecord::RecordInvalid: Validation failed: Orders count can't be blank +# => ActiveRecord::RecordInvalid: Validation failed: Orders count can't be blank </ruby> h4. +first_or_initialize+ @@ -1079,13 +1079,13 @@ The +first_or_initialize+ method will work just like +first_or_create+ but it wi <ruby> nick = Client.where(:first_name => 'Nick').first_or_initialize(:locked => false) -=> <Client id: nil, first_name: "Nick", orders_count: 0, locked: false, created_at: "2011-08-30 06:09:27", updated_at: "2011-08-30 06:09:27"> +# => <Client id: nil, first_name: "Nick", orders_count: 0, locked: false, created_at: "2011-08-30 06:09:27", updated_at: "2011-08-30 06:09:27"> nick.persisted? -=> false +# => false nick.new_record? -=> true +# => true </ruby> Because the object is not yet stored in the database, the SQL generated looks like this: @@ -1098,7 +1098,7 @@ When you want to save it to the database, just call +save+: <ruby> nick.save -=> true +# => true </ruby> h3. Finding by SQL |