rails 4.0.0 → 4.0.1.rc1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of rails might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/guides/CHANGELOG.md +6 -0
- data/guides/assets/images/akshaysurve.jpg +0 -0
- data/guides/bug_report_templates/action_controller_gem.rb +42 -0
- data/guides/bug_report_templates/action_controller_master.rb +51 -0
- data/guides/code/getting_started/Gemfile +1 -1
- data/guides/code/getting_started/Gemfile.lock +76 -106
- data/guides/code/getting_started/app/controllers/comments_controller.rb +7 -1
- data/guides/code/getting_started/app/controllers/posts_controller.rb +8 -2
- data/guides/code/getting_started/app/views/posts/_form.html.erb +3 -3
- data/guides/code/getting_started/app/views/welcome/index.html.erb +1 -0
- data/guides/code/getting_started/test/fixtures/comments.yml +1 -1
- data/guides/code/getting_started/test/fixtures/posts.yml +1 -1
- data/guides/rails_guides.rb +20 -1
- data/guides/source/3_0_release_notes.md +0 -2
- data/guides/source/4_0_release_notes.md +30 -13
- data/guides/source/_welcome.html.erb +4 -1
- data/guides/source/action_controller_overview.md +2 -2
- data/guides/source/action_mailer_basics.md +6 -21
- data/guides/source/action_view_overview.md +2 -71
- data/guides/source/active_record_basics.md +9 -9
- data/guides/source/active_record_callbacks.md +1 -0
- data/guides/source/active_record_querying.md +45 -5
- data/guides/source/active_record_validations.md +6 -9
- data/guides/source/active_support_core_extensions.md +50 -1
- data/guides/source/asset_pipeline.md +10 -4
- data/guides/source/association_basics.md +8 -4
- data/guides/source/command_line.md +8 -8
- data/guides/source/configuring.md +4 -2
- data/guides/source/contributing_to_ruby_on_rails.md +60 -10
- data/guides/source/credits.html.erb +5 -1
- data/guides/source/debugging_rails_applications.md +4 -2
- data/guides/source/documents.yaml +7 -0
- data/guides/source/form_helpers.md +1 -1
- data/guides/source/generators.md +21 -6
- data/guides/source/getting_started.md +61 -54
- data/guides/source/i18n.md +7 -6
- data/guides/source/layout.html.erb +6 -8
- data/guides/source/layouts_and_rendering.md +4 -4
- data/guides/source/maintenance_policy.md +56 -0
- data/guides/source/migrations.md +13 -11
- data/guides/source/plugins.md +9 -3
- data/guides/source/rails_on_rack.md +5 -1
- data/guides/source/security.md +1 -1
- data/guides/source/testing.md +83 -23
- data/guides/source/upgrading_ruby_on_rails.md +36 -12
- metadata +17 -13
data/guides/rails_guides.rb
CHANGED
@@ -22,7 +22,7 @@ end
|
|
22
22
|
|
23
23
|
begin
|
24
24
|
require 'redcarpet'
|
25
|
-
rescue
|
25
|
+
rescue LoadError
|
26
26
|
# This can happen if doc:guides is executed in an application.
|
27
27
|
$stderr.puts('Generating guides requires Redcarpet 2.1.1+.')
|
28
28
|
$stderr.puts(<<ERROR) if bundler?
|
@@ -39,6 +39,25 @@ ERROR
|
|
39
39
|
exit 1
|
40
40
|
end
|
41
41
|
|
42
|
+
begin
|
43
|
+
require 'nokogiri'
|
44
|
+
rescue LoadError
|
45
|
+
# This can happen if doc:guides is executed in an application.
|
46
|
+
$stderr.puts('Generating guides requires Nokogiri.')
|
47
|
+
$stderr.puts(<<ERROR) if bundler?
|
48
|
+
Please add
|
49
|
+
|
50
|
+
gem 'nokogiri'
|
51
|
+
|
52
|
+
to the Gemfile, run
|
53
|
+
|
54
|
+
bundle install
|
55
|
+
|
56
|
+
and try again.
|
57
|
+
ERROR
|
58
|
+
exit 1
|
59
|
+
end
|
60
|
+
|
42
61
|
require 'rails_guides/markdown'
|
43
62
|
require "rails_guides/generator"
|
44
63
|
RailsGuides::Generator.new.generate
|
@@ -73,8 +73,6 @@ You can see an example of how that works at [Rails Upgrade is now an Official Pl
|
|
73
73
|
|
74
74
|
Aside from Rails Upgrade tool, if you need more help, there are people on IRC and [rubyonrails-talk](http://groups.google.com/group/rubyonrails-talk) that are probably doing the same thing, possibly hitting the same issues. Be sure to blog your own experiences when upgrading so others can benefit from your knowledge!
|
75
75
|
|
76
|
-
More information - [The Path to Rails 3: Approaching the upgrade](http://omgbloglol.com/post/353978923/the-path-to-rails-3-approaching-the-upgrade)
|
77
|
-
|
78
76
|
Creating a Rails 3.0 application
|
79
77
|
--------------------------------
|
80
78
|
|
@@ -52,22 +52,22 @@ Major Features
|
|
52
52
|
|
53
53
|
TODO. Give a list and then talk about each of them briefly. We can point to relevant code commits or documentation from these sections.
|
54
54
|
|
55
|
-
![Rails 4.0](images/rails4_features.png)
|
55
|
+
[![Rails 4.0](images/rails4_features.png)](http://guides.rubyonrails.org/images/rails4_features.png)
|
56
56
|
|
57
57
|
Extraction of features to gems
|
58
58
|
---------------------------
|
59
59
|
|
60
60
|
In Rails 4.0, several features have been extracted into gems. You can simply add the extracted gems to your `Gemfile` to bring the functionality back.
|
61
61
|
|
62
|
-
* Hash-based & Dynamic finder methods ([
|
63
|
-
* Mass assignment protection in Active Record models ([
|
64
|
-
* ActiveRecord::SessionStore ([
|
65
|
-
* Active Record Observers ([
|
66
|
-
* Active Resource ([
|
67
|
-
* Action Caching ([
|
68
|
-
* Page Caching ([
|
69
|
-
* Sprockets ([
|
70
|
-
* Performance tests ([
|
62
|
+
* Hash-based & Dynamic finder methods ([GitHub](https://github.com/rails/activerecord-deprecated_finders))
|
63
|
+
* Mass assignment protection in Active Record models ([GitHub](https://github.com/rails/protected_attributes), [Pull Request](https://github.com/rails/rails/pull/7251))
|
64
|
+
* ActiveRecord::SessionStore ([GitHub](https://github.com/rails/activerecord-session_store), [Pull Request](https://github.com/rails/rails/pull/7436))
|
65
|
+
* Active Record Observers ([GitHub](https://github.com/rails/rails-observers), [Commit](https://github.com/rails/rails/commit/39e85b3b90c58449164673909a6f1893cba290b2))
|
66
|
+
* Active Resource ([GitHub](https://github.com/rails/activeresource), [Pull Request](https://github.com/rails/rails/pull/572), [Blog](http://yetimedia.tumblr.com/post/35233051627/activeresource-is-dead-long-live-activeresource))
|
67
|
+
* Action Caching ([GitHub](https://github.com/rails/actionpack-action_caching), [Pull Request](https://github.com/rails/rails/pull/7833))
|
68
|
+
* Page Caching ([GitHub](https://github.com/rails/actionpack-page_caching), [Pull Request](https://github.com/rails/rails/pull/7833))
|
69
|
+
* Sprockets ([GitHub](https://github.com/rails/sprockets-rails))
|
70
|
+
* Performance tests ([GitHub](https://github.com/rails/rails-perftest), [Pull Request](https://github.com/rails/rails/pull/8876))
|
71
71
|
|
72
72
|
Documentation
|
73
73
|
-------------
|
@@ -89,6 +89,10 @@ Please refer to the [Changelog](https://github.com/rails/rails/blob/master/railt
|
|
89
89
|
|
90
90
|
* Threadsafe on by default
|
91
91
|
|
92
|
+
* Ability to use a custom builder by passing `--builder` (or `-b`) to
|
93
|
+
`rails new` has been removed. Consider using application templates
|
94
|
+
instead. ([Pull Request](https://github.com/rails/rails/pull/9401))
|
95
|
+
|
92
96
|
### Deprecations
|
93
97
|
|
94
98
|
* `config.threadsafe!` is deprecated in favor of `config.eager_load` which provides a more fine grained control on what is eager loaded.
|
@@ -133,17 +137,30 @@ Please refer to the [Changelog](https://github.com/rails/rails/blob/master/activ
|
|
133
137
|
|
134
138
|
* `Object#try` will now return nil instead of raise a NoMethodError if the receiving object does not implement the method, but you can still get the old behavior by using the new `Object#try!`.
|
135
139
|
|
140
|
+
* `String#to_date` now raises `Argument Error: invalid date` instead of `NoMethodError: undefined method 'div' for nil:NilClass`
|
141
|
+
when given an invalid date. It is now the same as `Date.parse`, and it accepts more invalid dates than 3.x, such as:
|
142
|
+
|
143
|
+
```
|
144
|
+
# ActiveSupport 3.x
|
145
|
+
"asdf".to_date # => NoMethodError: undefined method `div' for nil:NilClass
|
146
|
+
"333".to_date # => NoMethodError: undefined method `div' for nil:NilClass
|
147
|
+
|
148
|
+
# ActiveSupport 4
|
149
|
+
"asdf".to_date # => ArgumentError: invalid date
|
150
|
+
"333".to_date # => Fri, 29 Nov 2013
|
151
|
+
```
|
152
|
+
|
136
153
|
### Deprecations
|
137
154
|
|
138
155
|
* Deprecate `ActiveSupport::TestCase#pending` method, use `skip` from MiniTest instead.
|
139
156
|
|
140
|
-
* ActiveSupport::Benchmarkable#silence has been deprecated due to its lack of thread safety. It will be removed without replacement in Rails 4.1.
|
157
|
+
* `ActiveSupport::Benchmarkable#silence` has been deprecated due to its lack of thread safety. It will be removed without replacement in Rails 4.1.
|
141
158
|
|
142
159
|
* `ActiveSupport::JSON::Variable` is deprecated. Define your own `#as_json` and `#encode_json` methods for custom JSON string literals.
|
143
160
|
|
144
|
-
* Deprecates the compatibility method Module#local_constant_names
|
161
|
+
* Deprecates the compatibility method `Module#local_constant_names`, use `Module#local_constants` instead (which returns symbols).
|
145
162
|
|
146
|
-
* BufferedLogger is deprecated.
|
163
|
+
* `BufferedLogger` is deprecated. Use `ActiveSupport::Logger`, or the logger from Ruby standard library.
|
147
164
|
|
148
165
|
* Deprecate `assert_present` and `assert_blank` in favor of `assert object.blank?` and `assert object.present?`
|
149
166
|
|
@@ -10,10 +10,13 @@
|
|
10
10
|
</p>
|
11
11
|
<% else %>
|
12
12
|
<p>
|
13
|
-
These are the new guides for Rails
|
13
|
+
These are the new guides for Rails 4.0 based on <a href="https://github.com/rails/rails/tree/<%= @version %>"><%= @version %></a>.
|
14
14
|
These guides are designed to make you immediately productive with Rails, and to help you understand how all of the pieces fit together.
|
15
15
|
</p>
|
16
16
|
<% end %>
|
17
|
+
<p>
|
18
|
+
The guides for Rails 3.2.x are available at <a href="http://guides.rubyonrails.org/v3.2.14/">http://guides.rubyonrails.org/v3.2.14/</a>.
|
19
|
+
</p>
|
17
20
|
<p>
|
18
21
|
The guides for Rails 2.3.x are available at <a href="http://guides.rubyonrails.org/v2.3.11/">http://guides.rubyonrails.org/v2.3.11/</a>.
|
19
22
|
</p>
|
@@ -160,7 +160,7 @@ NOTE: Support for parsing XML parameters has been extracted into a gem named `ac
|
|
160
160
|
The `params` hash will always contain the `:controller` and `:action` keys, but you should use the methods `controller_name` and `action_name` instead to access these values. Any other parameters defined by the routing, such as `:id` will also be available. As an example, consider a listing of clients where the list can show either active or inactive clients. We can add a route which captures the `:status` parameter in a "pretty" URL:
|
161
161
|
|
162
162
|
```ruby
|
163
|
-
|
163
|
+
get '/clients/:status' => 'clients#index', foo: 'bar'
|
164
164
|
```
|
165
165
|
|
166
166
|
In this case, when a user opens the URL `/clients/active`, `params[:status]` will be set to "active". When this route is used, `params[:foo]` will also be set to "bar" just like it was passed in the query string. In the same way `params[:action]` will contain "index".
|
@@ -410,7 +410,7 @@ class ApplicationController < ActionController::Base
|
|
410
410
|
# logging out removes it.
|
411
411
|
def current_user
|
412
412
|
@_current_user ||= session[:current_user_id] &&
|
413
|
-
User.
|
413
|
+
User.find_by(id: session[:current_user_id])
|
414
414
|
end
|
415
415
|
end
|
416
416
|
```
|
@@ -216,6 +216,11 @@ Action Mailer makes it very easy to add attachments.
|
|
216
216
|
attachments['filename.jpg'] = File.read('/path/to/filename.jpg')
|
217
217
|
```
|
218
218
|
|
219
|
+
When the `mail` method will be triggered, it will send a multipart email with
|
220
|
+
an attachment, properly nested with the top level being `multipart/mixed` and
|
221
|
+
the first part being a `multipart/alternative` containing the plain text and
|
222
|
+
HTML email messages.
|
223
|
+
|
219
224
|
NOTE: Mail will automatically Base64 encode an attachment. If you want something
|
220
225
|
different, encode your content and pass in the encoded content and encoding in a
|
221
226
|
`Hash` to the `attachments` method.
|
@@ -451,26 +456,6 @@ with the HTML and text versions setup as different parts.
|
|
451
456
|
The order of the parts getting inserted is determined by the `:parts_order`
|
452
457
|
inside of the `ActionMailer::Base.default` method.
|
453
458
|
|
454
|
-
### Sending Emails with Attachments
|
455
|
-
|
456
|
-
Attachments can be added by using the `attachments` method:
|
457
|
-
|
458
|
-
```ruby
|
459
|
-
class UserMailer < ActionMailer::Base
|
460
|
-
def welcome_email(user)
|
461
|
-
@user = user
|
462
|
-
@url = user_url(@user)
|
463
|
-
attachments['terms.pdf'] = File.read('/path/terms.pdf')
|
464
|
-
mail(to: @user.email,
|
465
|
-
subject: 'Please see the Terms and Conditions attached')
|
466
|
-
end
|
467
|
-
end
|
468
|
-
```
|
469
|
-
|
470
|
-
The above will send a multipart email with an attachment, properly nested with
|
471
|
-
the top level being `multipart/mixed` and the first part being a
|
472
|
-
`multipart/alternative` containing the plain text and HTML email messages.
|
473
|
-
|
474
459
|
### Sending Emails with Dynamic Delivery Options
|
475
460
|
|
476
461
|
If you wish to override the default delivery options (e.g. SMTP credentials)
|
@@ -532,7 +517,7 @@ method. Here's an example:
|
|
532
517
|
```ruby
|
533
518
|
class UserMailer < ActionMailer::Base
|
534
519
|
def receive(email)
|
535
|
-
page = Page.
|
520
|
+
page = Page.find_by(address: email.to.first)
|
536
521
|
page.emails.create(
|
537
522
|
subject: email.subject,
|
538
523
|
body: email.body
|
@@ -269,7 +269,7 @@ Rails will render the `_product_ruler` partial (with no data passed to it) betwe
|
|
269
269
|
|
270
270
|
### Layouts
|
271
271
|
|
272
|
-
Layouts can be used to render a common view template around the results of Rails controller actions. Typically, every Rails has a couple of overall layouts that most pages are rendered within. For example, a site might have a layout for a logged in user, and a layout for the marketing or sales side of the site. The logged in user layout might include top-level navigation that should be present across many controller actions. The sales layout for a SaaS app might include top-level navigation for things like "Pricing" and "Contact Us." You would expect each layout to have a different look and feel. You can read more details about Layouts in the [Layouts and Rendering in Rails](layouts_and_rendering.html) guide.
|
272
|
+
Layouts can be used to render a common view template around the results of Rails controller actions. Typically, every Rails application has a couple of overall layouts that most pages are rendered within. For example, a site might have a layout for a logged in user, and a layout for the marketing or sales side of the site. The logged in user layout might include top-level navigation that should be present across many controller actions. The sales layout for a SaaS app might include top-level navigation for things like "Pricing" and "Contact Us." You would expect each layout to have a different look and feel. You can read more details about Layouts in the [Layouts and Rendering in Rails](layouts_and_rendering.html) guide.
|
273
273
|
|
274
274
|
Partial Layouts
|
275
275
|
---------------
|
@@ -1090,7 +1090,7 @@ Example object structure for use with this method:
|
|
1090
1090
|
|
1091
1091
|
```ruby
|
1092
1092
|
class Post < ActiveRecord::Base
|
1093
|
-
has_and_belongs_to_many :
|
1093
|
+
has_and_belongs_to_many :authors
|
1094
1094
|
end
|
1095
1095
|
|
1096
1096
|
class Author < ActiveRecord::Base
|
@@ -1489,72 +1489,3 @@ end
|
|
1489
1489
|
Then you could create special views like `app/views/posts/show.expert.html.erb` that would only be displayed to expert users.
|
1490
1490
|
|
1491
1491
|
You can read more about the Rails Internationalization (I18n) API [here](i18n.html).
|
1492
|
-
|
1493
|
-
Using Action View outside of Rails
|
1494
|
-
----------------------------------
|
1495
|
-
|
1496
|
-
Action View is a Rails component, but it can also be used without Rails. We can demonstrate this by creating a small [Rack](http://rack.rubyforge.org/) application that includes Action View functionality. This may be useful, for example, if you'd like access to Action View's helpers in a Rack application.
|
1497
|
-
|
1498
|
-
Let's start by ensuring that you have the Action Pack and Rack gems installed:
|
1499
|
-
|
1500
|
-
```bash
|
1501
|
-
$ gem install actionpack
|
1502
|
-
$ gem install rack
|
1503
|
-
```
|
1504
|
-
|
1505
|
-
Now we'll create a simple "Hello World" application that uses the `titleize` method provided by Active Support.
|
1506
|
-
|
1507
|
-
**hello_world.rb:**
|
1508
|
-
|
1509
|
-
```ruby
|
1510
|
-
require 'active_support/core_ext/string/inflections'
|
1511
|
-
require 'rack'
|
1512
|
-
|
1513
|
-
def hello_world(env)
|
1514
|
-
[200, {"Content-Type" => "text/html"}, "hello world".titleize]
|
1515
|
-
end
|
1516
|
-
|
1517
|
-
Rack::Handler::Mongrel.run method(:hello_world), Port: 4567
|
1518
|
-
```
|
1519
|
-
|
1520
|
-
We can see this all come together by starting up the application and then visiting `http://localhost:4567/`
|
1521
|
-
|
1522
|
-
```bash
|
1523
|
-
$ ruby hello_world.rb
|
1524
|
-
```
|
1525
|
-
|
1526
|
-
TODO needs a screenshot? I have one - not sure where to put it.
|
1527
|
-
|
1528
|
-
Notice how 'hello world' has been converted into 'Hello World' by the `titleize` helper method.
|
1529
|
-
|
1530
|
-
Action View can also be used with [Sinatra](http://www.sinatrarb.com/) in the same way.
|
1531
|
-
|
1532
|
-
Let's start by ensuring that you have the Action Pack and Sinatra gems installed:
|
1533
|
-
|
1534
|
-
```bash
|
1535
|
-
$ gem install actionpack
|
1536
|
-
$ gem install sinatra
|
1537
|
-
```
|
1538
|
-
|
1539
|
-
Now we'll create the same "Hello World" application in Sinatra.
|
1540
|
-
|
1541
|
-
**hello_world.rb:**
|
1542
|
-
|
1543
|
-
```ruby
|
1544
|
-
require 'action_view'
|
1545
|
-
require 'sinatra'
|
1546
|
-
|
1547
|
-
get '/' do
|
1548
|
-
erb 'hello world'.titleize
|
1549
|
-
end
|
1550
|
-
```
|
1551
|
-
|
1552
|
-
Then, we can run the application:
|
1553
|
-
|
1554
|
-
```bash
|
1555
|
-
$ ruby hello_world.rb
|
1556
|
-
```
|
1557
|
-
|
1558
|
-
Once the application is running, you can see Sinatra and Action View working together by visiting `http://localhost:4567/`
|
1559
|
-
|
1560
|
-
TODO needs a screenshot? I have one - not sure where to put it.
|
@@ -105,7 +105,7 @@ depending on the purpose of these columns.
|
|
105
105
|
Migrations](migrations.html) to create your tables, this column will be
|
106
106
|
automatically created.
|
107
107
|
|
108
|
-
There are also some optional column names that will
|
108
|
+
There are also some optional column names that will add additional features
|
109
109
|
to Active Record instances:
|
110
110
|
|
111
111
|
* `created_at` - Automatically gets set to the current date and time when the
|
@@ -116,7 +116,7 @@ to Active Record instances:
|
|
116
116
|
locking](http://api.rubyonrails.org/classes/ActiveRecord/Locking.html) to
|
117
117
|
a model.
|
118
118
|
* `type` - Specifies that the model uses [Single Table
|
119
|
-
Inheritance](http://api.rubyonrails.org/classes/ActiveRecord/Base.html)
|
119
|
+
Inheritance](http://api.rubyonrails.org/classes/ActiveRecord/Base.html#label-Single+table+inheritance).
|
120
120
|
* `(association_name)_type` - Stores the type for
|
121
121
|
[polymorphic associations](association_basics.html#polymorphic-associations).
|
122
122
|
* `(table_name)_count` - Used to cache the number of belonging objects on
|
@@ -188,11 +188,11 @@ end
|
|
188
188
|
```
|
189
189
|
|
190
190
|
It's also possible to override the column that should be used as the table's
|
191
|
-
primary key using the `ActiveRecord::Base.
|
191
|
+
primary key using the `ActiveRecord::Base.primary_key=` method:
|
192
192
|
|
193
193
|
```ruby
|
194
194
|
class Product < ActiveRecord::Base
|
195
|
-
|
195
|
+
self.primary_key = "product_id"
|
196
196
|
end
|
197
197
|
```
|
198
198
|
|
@@ -253,7 +253,7 @@ user = User.first
|
|
253
253
|
|
254
254
|
```ruby
|
255
255
|
# return the first user named David
|
256
|
-
david = User.
|
256
|
+
david = User.find_by(name: 'David')
|
257
257
|
```
|
258
258
|
|
259
259
|
```ruby
|
@@ -270,7 +270,7 @@ Once an Active Record object has been retrieved, its attributes can be modified
|
|
270
270
|
and it can be saved to the database.
|
271
271
|
|
272
272
|
```ruby
|
273
|
-
user = User.
|
273
|
+
user = User.find_by(name: 'David')
|
274
274
|
user.name = 'Dave'
|
275
275
|
user.save
|
276
276
|
```
|
@@ -279,7 +279,7 @@ A shorthand for this is to use a hash mapping attribute names to the desired
|
|
279
279
|
value, like so:
|
280
280
|
|
281
281
|
```ruby
|
282
|
-
user = User.
|
282
|
+
user = User.find_by(name: 'David')
|
283
283
|
user.update(name: 'Dave')
|
284
284
|
```
|
285
285
|
|
@@ -297,7 +297,7 @@ Likewise, once retrieved an Active Record object can be destroyed which removes
|
|
297
297
|
it from the database.
|
298
298
|
|
299
299
|
```ruby
|
300
|
-
user = User.
|
300
|
+
user = User.find_by(name: 'David')
|
301
301
|
user.destroy
|
302
302
|
```
|
303
303
|
|
@@ -343,7 +343,7 @@ Migrations
|
|
343
343
|
|
344
344
|
Rails provides a domain-specific language for managing a database schema called
|
345
345
|
migrations. Migrations are stored in files which are executed against any
|
346
|
-
database that Active Record
|
346
|
+
database that Active Record supports using `rake`. Here's a migration that
|
347
347
|
creates a table:
|
348
348
|
|
349
349
|
```ruby
|
@@ -524,12 +524,18 @@ To retrieve records from the database in a specific order, you can use the `orde
|
|
524
524
|
For example, if you're getting a set of records and want to order them in ascending order by the `created_at` field in your table:
|
525
525
|
|
526
526
|
```ruby
|
527
|
+
Client.order(:created_at)
|
528
|
+
# OR
|
527
529
|
Client.order("created_at")
|
528
530
|
```
|
529
531
|
|
530
532
|
You could specify `ASC` or `DESC` as well:
|
531
533
|
|
532
534
|
```ruby
|
535
|
+
Client.order(created_at: :desc)
|
536
|
+
# OR
|
537
|
+
Client.order(created_at: :asc)
|
538
|
+
# OR
|
533
539
|
Client.order("created_at DESC")
|
534
540
|
# OR
|
535
541
|
Client.order("created_at ASC")
|
@@ -538,16 +544,20 @@ Client.order("created_at ASC")
|
|
538
544
|
Or ordering by multiple fields:
|
539
545
|
|
540
546
|
```ruby
|
547
|
+
Client.order(orders_count: :asc, created_at: :desc)
|
548
|
+
# OR
|
549
|
+
Client.order(:orders_count, created_at: :desc)
|
550
|
+
# OR
|
541
551
|
Client.order("orders_count ASC, created_at DESC")
|
542
552
|
# OR
|
543
553
|
Client.order("orders_count ASC", "created_at DESC")
|
544
554
|
```
|
545
555
|
|
546
|
-
If you want to call `order` multiple times e.g. in different context, new order will
|
556
|
+
If you want to call `order` multiple times e.g. in different context, new order will append previous one
|
547
557
|
|
548
558
|
```ruby
|
549
559
|
Client.order("orders_count ASC").order("created_at DESC")
|
550
|
-
# SELECT * FROM clients ORDER BY
|
560
|
+
# SELECT * FROM clients ORDER BY orders_count ASC, created_at DESC
|
551
561
|
```
|
552
562
|
|
553
563
|
Selecting Specific Fields
|
@@ -1203,7 +1213,6 @@ class User < ActiveRecord::Base
|
|
1203
1213
|
scope :inactive, -> { where state: 'inactive' }
|
1204
1214
|
end
|
1205
1215
|
|
1206
|
-
```ruby
|
1207
1216
|
User.active.inactive
|
1208
1217
|
# => SELECT "users".* FROM "users" WHERE "users"."state" = 'active' AND "users"."state" = 'inactive'
|
1209
1218
|
```
|
@@ -1449,7 +1458,7 @@ Client.pluck(:id, :name)
|
|
1449
1458
|
# => [[1, 'David'], [2, 'Jeremy'], [3, 'Jose']]
|
1450
1459
|
```
|
1451
1460
|
|
1452
|
-
`pluck` makes it possible to replace code like
|
1461
|
+
`pluck` makes it possible to replace code like:
|
1453
1462
|
|
1454
1463
|
```ruby
|
1455
1464
|
Client.select(:id).map { |c| c.id }
|
@@ -1459,7 +1468,7 @@ Client.select(:id).map(&:id)
|
|
1459
1468
|
Client.select(:id, :name).map { |c| [c.id, c.name] }
|
1460
1469
|
```
|
1461
1470
|
|
1462
|
-
with
|
1471
|
+
with:
|
1463
1472
|
|
1464
1473
|
```ruby
|
1465
1474
|
Client.pluck(:id)
|
@@ -1467,6 +1476,37 @@ Client.pluck(:id)
|
|
1467
1476
|
Client.pluck(:id, :name)
|
1468
1477
|
```
|
1469
1478
|
|
1479
|
+
Unlike `select`, `pluck` directly converts a database result into a Ruby `Array`,
|
1480
|
+
without constructing `ActiveRecord` objects. This can mean better performance for
|
1481
|
+
a large or often-running query. However, any model method overrides will
|
1482
|
+
not be available. For example:
|
1483
|
+
|
1484
|
+
```ruby
|
1485
|
+
class Client < ActiveRecord::Base
|
1486
|
+
def name
|
1487
|
+
"I am #{super}"
|
1488
|
+
end
|
1489
|
+
end
|
1490
|
+
|
1491
|
+
Client.select(:name).map &:name
|
1492
|
+
# => ["I am David", "I am Jeremy", "I am Jose"]
|
1493
|
+
|
1494
|
+
Client.pluck(:name)
|
1495
|
+
# => ["David", "Jeremy", "Jose"]
|
1496
|
+
```
|
1497
|
+
|
1498
|
+
Furthermore, unlike `select` and other `Relation` scopes, `pluck` triggers an immediate
|
1499
|
+
query, and thus cannot be chained with any further scopes, although it can work with
|
1500
|
+
scopes already constructed earlier:
|
1501
|
+
|
1502
|
+
```ruby
|
1503
|
+
Client.pluck(:name).limit(1)
|
1504
|
+
# => NoMethodError: undefined method `limit' for #<Array:0x007ff34d3ad6d8>
|
1505
|
+
|
1506
|
+
Client.limit(1).pluck(:name)
|
1507
|
+
# => ["David"]
|
1508
|
+
```
|
1509
|
+
|
1470
1510
|
### `ids`
|
1471
1511
|
|
1472
1512
|
`ids` can be used to pluck all the IDs for the relation using the table's primary key.
|