Skip to content

Commit 8b2e4fd

Browse files
committed
Merge remote branch 'docrails/master'
2 parents db6190a + 28474a7 commit 8b2e4fd

File tree

4 files changed

+52
-57
lines changed

4 files changed

+52
-57
lines changed

actionpack/lib/action_view/helpers/url_helper.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,9 @@ def link_to(*args, &block)
269269
# The +options+ hash accepts the same options as url_for.
270270
#
271271
# There are a few special +html_options+:
272-
# * <tt>:method</tt> - Specifies the anchor name to be appended to the path.
273-
# * <tt>:disabled</tt> - Specifies the anchor name to be appended to the path.
272+
# * <tt>:method</tt> - Symbol of HTTP verb. Supported verbs are <tt>:post</tt>, <tt>:get</tt>,
273+
# <tt>:delete</tt> and <tt>:put</tt>. By default it will be <tt>:post</tt>.
274+
# * <tt>:disabled</tt> - If set to true, it will generate a disabled button.
274275
# * <tt>:confirm</tt> - This will use the unobtrusive JavaScript driver to
275276
# prompt with the question specified. If the user accepts, the link is
276277
# processed normally, otherwise no action is taken.

railties/guides/source/active_record_querying.textile

Lines changed: 42 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -234,23 +234,23 @@ h4. Array Conditions
234234
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:
235235

236236
<ruby>
237-
Client.where(["orders_count = ?", params[:orders]])
237+
Client.where("orders_count = ?", params[:orders])
238238
</ruby>
239239

240240
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.
241241

242242
Or if you want to specify two conditions, you can do it like:
243243

244244
<ruby>
245-
Client.where(["orders_count = ? AND locked = ?", params[:orders], false])
245+
Client.where("orders_count = ? AND locked = ?", params[:orders], false)
246246
</ruby>
247247

248248
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.
249249

250250
The reason for doing code like:
251251

252252
<ruby>
253-
Client.where(["orders_count = ?", params[:orders]])
253+
Client.where("orders_count = ?", params[:orders])
254254
</ruby>
255255

256256
instead of:
@@ -268,8 +268,8 @@ h5. Placeholder Conditions
268268
Similar to the +(?)+ replacement style of params, you can also specify keys/values hash in your array conditions:
269269

270270
<ruby>
271-
Client.where(
272-
["created_at >= :start_date AND created_at <= :end_date", { :start_date => params[:start_date], :end_date => params[:end_date] }])
271+
Client.where("created_at >= :start_date AND created_at <= :end_date",
272+
{:start_date => params[:start_date], :end_date => params[:end_date]})
273273
</ruby>
274274

275275
This makes for clearer readability if you have a large number of variable conditions.
@@ -279,8 +279,8 @@ h5(#array-range_conditions). Range Conditions
279279
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:
280280

281281
<ruby>
282-
Client.where(["created_at IN (?)",
283-
(params[:start_date].to_date)..(params[:end_date].to_date)])
282+
Client.where("created_at IN (?)",
283+
(params[:start_date].to_date)..(params[:end_date].to_date))
284284
</ruby>
285285

286286
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
301301
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:
302302

303303
<ruby>
304-
Client.where(["created_at IN (?)",
305-
(params[:start_date].to_date.to_time)..(params[:end_date].to_date.to_time)])
304+
Client.where("created_at IN (?)",
305+
(params[:start_date].to_date.to_time)..(params[:end_date].to_date.to_time))
306306
</ruby>
307307

308308
<sql>
@@ -323,14 +323,14 @@ In this example it would be better to use greater-than and less-than operators i
323323

324324
<ruby>
325325
Client.where(
326-
["created_at > ? AND created_at < ?", params[:start_date], params[:end_date]])
326+
"created_at > ? AND created_at < ?", params[:start_date], params[:end_date])
327327
</ruby>
328328

329329
You can also use the greater-than-or-equal-to and less-than-or-equal-to like this:
330330

331331
<ruby>
332332
Client.where(
333-
["created_at >= ? AND created_at <= ?", params[:start_date], params[:end_date]])
333+
"created_at >= ? AND created_at <= ?", params[:start_date], params[:end_date])
334334
</ruby>
335335

336336
Just like in Ruby. If you want a shorter syntax be sure to check out the "Hash Conditions":#hash-conditions section later on in the guide.
@@ -344,21 +344,21 @@ NOTE: Only equality, range and subset checking are possible with Hash conditions
344344
h5. Equality Conditions
345345

346346
<ruby>
347-
Client.where({ :locked => true })
347+
Client.where(:locked => true)
348348
</ruby>
349349

350350
The field name can also be a string:
351351

352352
<ruby>
353-
Client.where({ 'locked' => true })
353+
Client.where('locked' => true)
354354
</ruby>
355355

356356
h5(#hash-range_conditions). Range Conditions
357357

358358
The good thing about this is that we can pass in a range for our fields without it generating a large query as shown in the preamble of this section.
359359

360360
<ruby>
361-
Client.where({ :created_at => (Time.now.midnight - 1.day)..Time.now.midnight})
361+
Client.where(:created_at => (Time.now.midnight - 1.day)..Time.now.midnight)
362362
</ruby>
363363

364364
This will find all clients created yesterday by using a +BETWEEN+ SQL statement:
@@ -374,7 +374,7 @@ h5. Subset Conditions
374374
If you want to find records using the +IN+ expression you can pass an array to the conditions hash:
375375

376376
<ruby>
377-
Client.where({ :orders_count => [1,3,5] })
377+
Client.where(:orders_count => [1,3,5])
378378
</ruby>
379379

380380
This code will generate SQL like this:
@@ -496,7 +496,7 @@ SQL uses the +HAVING+ clause to specify conditions on the +GROUP BY+ fields. You
496496
For example:
497497

498498
<ruby>
499-
Order.group("date(created_at)".having(["created_at > ?", 1.month.ago])
499+
Order.group("date(created_at)".having("created_at > ?", 1.month.ago)
500500
</ruby>
501501

502502
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.
509509

510510
h4. Readonly Objects
511511

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.
515513

516514
<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
525517
client.save
526518
</ruby>
527519

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+
528522
h4. Locking Records for Update
529523

530524
Locking is helpful for preventing race conditions when updating records in the database and ensuring atomic updates.
@@ -571,13 +565,13 @@ end
571565

572566
h5. Pessimistic Locking
573567

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.
575569

576570
For example:
577571

578572
<ruby>
579573
Item.transaction do
580-
i = Item.first(:lock => true)
574+
i = Item.lock.first
581575
i.name = 'Jones'
582576
i.save
583577
end
@@ -592,25 +586,25 @@ Item Update (0.4ms) UPDATE `items` SET `updated_at` = '2009-02-07 18:05:56', `
592586
SQL (0.8ms) COMMIT
593587
</sql>
594588

595-
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:
596590

597591
<ruby>
598592
Item.transaction do
599-
i = Item.find(1, :lock => "LOCK IN SHARE MODE")
593+
i = Item.lock("LOCK IN SHARE MODE").find(1)
600594
i.increment!(:views)
601595
end
602596
</ruby>
603597

604598
h3. Joining Tables
605599

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.
607601

608602
h4. Using a String SQL Fragment
609603

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+:
611605

612606
<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')
614608
</ruby>
615609

616610
This will result in the following SQL:
@@ -625,7 +619,7 @@ WARNING: This method only works with +INNER JOIN+,
625619

626620
<br />
627621

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.
629623

630624
For example, consider the following +Category+, +Post+, +Comments+ and +Guest+ models:
631625

@@ -838,13 +832,13 @@ Client.exists?(1,2,3)
838832
Client.exists?([1,2,3])
839833
</ruby>
840834

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.
842836

843837
<ruby>
844-
Client.exists?(:conditions => "first_name = 'Ryan'")
838+
Client.where(:first_name => 'Ryan').exists?
845839
</ruby>
846840

847-
It's even possible to use +exists?+ without any arguments:
841+
The above returns +true+ if there is at least one client with the +first_name+ 'Ryan' and +false+ otherwise.
848842

849843
<ruby>
850844
Client.exists?
@@ -856,22 +850,24 @@ h3. Calculations
856850

857851
This section uses count as an example method in this preamble, but the options described apply to all sub-sections.
858852

859-
<tt>count</tt> takes conditions much in the same way +exists?+ does:
853+
All calculation methods work directly on a model:
860854

861855
<ruby>
862-
Client.count(:conditions => "first_name = 'Ryan'")
856+
Client.count
857+
# SELECT count(*) AS count_all FROM clients
863858
</ruby>
864859

865-
Which will execute:
860+
Or on a relation :
866861

867-
<sql>
868-
SELECT count(*) AS count_all FROM clients WHERE (first_name = 'Ryan')
869-
</sql>
862+
<ruby>
863+
Client.where(:first_name => 'Ryan').count
864+
# SELECT count(*) AS count_all FROM clients WHERE (first_name = 'Ryan')
865+
</ruby>
870866

871-
You can also use the +includes+ or +joins+ methods for this to do something a little more complex:
867+
You can also use various finder methods on a relation for performing complex calculations:
872868

873869
<ruby>
874-
Client.where("clients.first_name = 'Ryan' AND orders.status = 'received'").includes("orders").count
870+
Client.includes("orders").where(:first_name => 'Ryan', :orders => {:status => 'received'}).count
875871
</ruby>
876872

877873
Which will execute:
@@ -882,8 +878,6 @@ SELECT count(DISTINCT clients.id) AS count_all FROM clients
882878
(clients.first_name = 'Ryan' AND orders.status = 'received')
883879
</sql>
884880

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-
887881
h4. Count
888882

889883
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)+.

railties/guides/source/layouts_and_rendering.textile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1087,7 +1087,7 @@ Partials are very useful in rendering collections. When you pass a collection to
10871087

10881088
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.
10891089

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+:
10911091

10921092
<erb>
10931093
<h1>Products</h1>

railties/guides/source/performance_testing.textile

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ In a freshly generated Rails application, +test/performance/browsing_test.rb+ co
2020

2121
<ruby>
2222
require 'test_helper'
23-
require 'performance_test_help'
23+
require 'rails/performance_test_help'
2424

2525
# Profiling results for each test method are written to tmp/performance.
2626
class BrowsingTest < ActionController::PerformanceTest
@@ -34,17 +34,17 @@ This example is a simple performance test case for profiling a GET request to th
3434

3535
h4. Generating Performance Tests
3636

37-
Rails provides a generator called +performance_test+ for creating new performance tests:
37+
Rails provides a generator called +test_unit:performance+ for creating new performance tests:
3838

3939
<shell>
40-
rails generate performance_test homepage
40+
rails generate test_unit:performance homepage
4141
</shell>
4242

4343
This generates +homepage_test.rb+ in the +test/performance+ directory:
4444

4545
<ruby>
4646
require 'test_helper'
47-
require 'performance_test_help'
47+
require 'rails/performance_test_help'
4848

4949
class HomepageTest < ActionController::PerformanceTest
5050
# Replace this with your real tests.
@@ -103,7 +103,7 @@ Here's the performance test for +HomeController#dashboard+ and +PostsController#
103103

104104
<ruby>
105105
require 'test_helper'
106-
require 'performance_test_help'
106+
require 'rails/performance_test_help'
107107

108108
class PostPerformanceTest < ActionController::PerformanceTest
109109
def setup
@@ -131,7 +131,7 @@ Performance test for +Post+ model:
131131

132132
<ruby>
133133
require 'test_helper'
134-
require 'performance_test_help'
134+
require 'rails/performance_test_help'
135135

136136
class PostModelTest < ActionController::PerformanceTest
137137
def test_creation

0 commit comments

Comments
 (0)