You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: railties/guides/source/active_record_querying.textile
+42-48Lines changed: 42 additions & 48 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -234,23 +234,23 @@ h4. Array Conditions
234
234
Now what if that number could vary, say as an argument from somewhere, or perhaps from the user's level status somewhere? The find then becomes something like:
Active Record will go through the first element in the conditions value and any additional elements will replace the question marks +(?)+ in the first element.
241
241
242
242
Or if you want to specify two conditions, you can do it like:
243
243
244
244
<ruby>
245
-
Client.where(["orders_count = ? AND locked = ?", params[:orders], false])
245
+
Client.where("orders_count = ? AND locked = ?", params[:orders], false)
246
246
</ruby>
247
247
248
248
In this example, the first question mark will be replaced with the value in +params[:orders]+ and the second will be replaced with the SQL representation of +false+, which depends on the adapter.
This makes for clearer readability if you have a large number of variable conditions.
@@ -279,8 +279,8 @@ h5(#array-range_conditions). Range Conditions
279
279
If you're looking for a range inside of a table (for example, users created in a certain timeframe) you can use the conditions option coupled with the +IN+ SQL statement for this. If you had two dates coming in from a controller you could do something like this to look for a range:
This would generate the proper query which is great for small ranges but not so good for larger ranges. For example if you pass in a range of date objects spanning a year that's 365 (or possibly 366, depending on the year) strings it will attempt to match your field against.
@@ -301,8 +301,8 @@ h5. Time and Date Conditions
301
301
Things can get *really* messy if you pass in Time objects as it will attempt to compare your field to *every second* in that range:
The SQL that would be executed would be something like this:
@@ -509,22 +509,16 @@ This will return single order objects for each day, but only for the last month.
509
509
510
510
h4. Readonly Objects
511
511
512
-
To explicitly disallow modification/destruction of the matching records returned in a Relation object, you could chain the +readonly+ method as +true+ to the find call.
513
-
514
-
Any attempt to alter or destroy the readonly records will not succeed, raising an +ActiveRecord::ReadOnlyRecord+ exception. To set this option, specify it like this:
512
+
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.
515
513
516
514
<ruby>
517
-
Client.first.readonly(true)
518
-
</ruby>
519
-
520
-
For example, calling the following code will raise an +ActiveRecord::ReadOnlyRecord+ exception:
521
-
522
-
<ruby>
523
-
client = Client.first.readonly(true)
524
-
client.locked = false
515
+
client = Client.readonly.first
516
+
client.visits += 1
525
517
client.save
526
518
</ruby>
527
519
520
+
As +client+ is explicitly set to be a readonly object, the above code will raise an +ActiveRecord::ReadOnlyRecord+ exception when calling +client.save+ with an updated value of _visists_.
521
+
528
522
h4. Locking Records for Update
529
523
530
524
Locking is helpful for preventing race conditions when updating records in the database and ensuring atomic updates.
@@ -571,13 +565,13 @@ end
571
565
572
566
h5. Pessimistic Locking
573
567
574
-
Pessimistic locking uses a locking mechanism provided by the underlying database. Passing +:lock => true+ to +Model.find+ obtains an exclusive lock on the selected rows. +Model.find+ using +:lock+ are usually wrapped inside a transaction for preventing deadlock conditions.
568
+
Pessimistic locking uses a locking mechanism provided by the underlying database. Using +lock+ when building a relation obtains an exclusive lock on the selected rows. Relations using +lock+ are usually wrapped inside a transaction for preventing deadlock conditions.
You can also pass raw SQL to the +:lock+ option to allow different types of locks. For example, MySQL has an expression called +LOCK IN SHARE MODE+ where you can lock a record but still allow other queries to read it. To specify this expression just pass it in as the lock option:
589
+
You can also pass raw SQL to the +lock+ method for allowing different types of locks. For example, MySQL has an expression called +LOCK IN SHARE MODE+ where you can lock a record but still allow other queries to read it. To specify this expression just pass it in as the lock option:
596
590
597
591
<ruby>
598
592
Item.transaction do
599
-
i = Item.find(1, :lock => "LOCK IN SHARE MODE")
593
+
i = Item.lock("LOCK IN SHARE MODE").find(1)
600
594
i.increment!(:views)
601
595
end
602
596
</ruby>
603
597
604
598
h3. Joining Tables
605
599
606
-
<tt>Model.find</tt> provides a +:joins+ option for specifying +JOIN+ clauses on the resulting SQL. There are multiple ways to specify the +:joins+ option:
600
+
Active Record provides a finder method called +joins+ for specifying +JOIN+ clauses on the resulting SQL. There are multiple ways to use the +joins+ method.
607
601
608
602
h4. Using a String SQL Fragment
609
603
610
-
You can just supply the raw SQL specifying the +JOIN+ clause to the +:joins+ option. For example:
604
+
You can just supply the raw SQL specifying the +JOIN+ clause to +joins+:
611
605
612
606
<ruby>
613
-
Client.all(:joins => 'LEFT OUTER JOIN addresses ON addresses.client_id = clients.id')
607
+
Client.joins('LEFT OUTER JOIN addresses ON addresses.client_id = clients.id')
614
608
</ruby>
615
609
616
610
This will result in the following SQL:
@@ -625,7 +619,7 @@ WARNING: This method only works with +INNER JOIN+,
625
619
626
620
<br />
627
621
628
-
Active Record lets you use the names of the "associations":association_basics.html defined on the model as a shortcut for specifying the +:joins+ option.
622
+
Active Record lets you use the names of the "associations":association_basics.html defined on the model as a shortcut for specifying +JOIN+ clause for those associations when using the +joins+ method.
629
623
630
624
For example, consider the following +Category+, +Post+, +Comments+ and +Guest+ models:
631
625
@@ -838,13 +832,13 @@ Client.exists?(1,2,3)
838
832
Client.exists?([1,2,3])
839
833
</ruby>
840
834
841
-
The +exists+ method may also take a +conditions+ option much like find:
835
+
It's even possible to use +exists?+ without any arguments on a model or a relation.
@@ -882,8 +878,6 @@ SELECT count(DISTINCT clients.id) AS count_all FROM clients
882
878
(clients.first_name = 'Ryan' AND orders.status = 'received')
883
879
</sql>
884
880
885
-
This code specifies +clients.first_name+ just in case one of the join tables has a field also called +first_name+ and it uses +orders.status+ because that's the name of our join table.
886
-
887
881
h4. Count
888
882
889
883
If you want to see how many records are in your model's 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)+.
Copy file name to clipboardExpand all lines: railties/guides/source/layouts_and_rendering.textile
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1087,7 +1087,7 @@ Partials are very useful in rendering collections. When you pass a collection to
1087
1087
1088
1088
When a partial is called with a pluralized collection, then the individual instances of the partial have access to the member of the collection being rendered via a variable named after the partial. In this case, the partial is +_product+, and within the +_product+ partial, you can refer to +product+ to get the instance that is being rendered.
1089
1089
1090
-
In Rails 3.0 there is also a shorthand for this, assuming +@posts+ is a collection of +post+ instances, you can simply do in the +index.html.erb+:
1090
+
In Rails 3.0 there is also a shorthand for this, assuming +@products+ is a collection of +product+ instances, you can simply do in the +index.html.erb+:
0 commit comments