rails 2.1.2 → 2.2.2
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.
- data/CHANGELOG +27 -3
- data/Rakefile +70 -10
- data/bin/about +0 -0
- data/bin/console +0 -0
- data/bin/destroy +0 -0
- data/bin/generate +0 -0
- data/bin/performance/benchmarker +0 -0
- data/bin/performance/profiler +0 -0
- data/bin/performance/request +0 -0
- data/bin/plugin +0 -0
- data/bin/process/inspector +0 -0
- data/bin/process/reaper +0 -0
- data/bin/process/spawner +0 -0
- data/bin/runner +0 -0
- data/bin/server +0 -0
- data/config.ru +17 -0
- data/configs/apache.conf +0 -0
- data/configs/databases/ibm_db.yml +62 -0
- data/configs/databases/mysql.yml +3 -0
- data/configs/databases/postgresql.yml +3 -0
- data/configs/databases/sqlite2.yml +3 -0
- data/configs/databases/sqlite3.yml +3 -0
- data/configs/locales/en.yml +5 -0
- data/dispatches/dispatch.fcgi +1 -1
- data/dispatches/dispatch.rb +2 -2
- data/dispatches/gateway.cgi +2 -2
- data/doc/README_FOR_APP +5 -2
- data/doc/guides/html/2_2_release_notes.html +1185 -0
- data/doc/guides/html/actioncontroller_basics.html +1270 -0
- data/doc/guides/html/activerecord_validations_callbacks.html +749 -0
- data/doc/guides/html/association_basics.html +2585 -0
- data/doc/guides/html/authors.html +240 -0
- data/doc/guides/html/benchmarking_and_profiling.html +1018 -0
- data/doc/guides/html/caching_with_rails.html +583 -0
- data/doc/guides/html/command_line.html +434 -0
- data/doc/guides/html/configuring.html +438 -0
- data/doc/guides/html/creating_plugins.html +1594 -0
- data/doc/guides/html/debugging_rails_applications.html +1175 -0
- data/doc/guides/html/finders.html +1090 -0
- data/doc/guides/html/form_helpers.html +638 -0
- data/doc/guides/html/getting_started_with_rails.html +2066 -0
- data/doc/guides/html/index.html +349 -0
- data/doc/guides/html/layouts_and_rendering.html +1406 -0
- data/doc/guides/html/migrations.html +921 -0
- data/doc/guides/html/routing_outside_in.html +2213 -0
- data/doc/guides/html/security.html +1346 -0
- data/doc/guides/html/testing_rails_applications.html +1859 -0
- data/doc/guides/source/2_2_release_notes.txt +435 -0
- data/doc/guides/source/actioncontroller_basics/changelog.txt +5 -0
- data/doc/guides/source/actioncontroller_basics/cookies.txt +34 -0
- data/doc/guides/source/actioncontroller_basics/csrf.txt +32 -0
- data/doc/guides/source/actioncontroller_basics/filters.txt +119 -0
- data/doc/guides/source/actioncontroller_basics/http_auth.txt +24 -0
- data/doc/guides/source/actioncontroller_basics/index.txt +40 -0
- data/doc/guides/source/actioncontroller_basics/introduction.txt +9 -0
- data/doc/guides/source/actioncontroller_basics/methods.txt +39 -0
- data/doc/guides/source/actioncontroller_basics/parameter_filtering.txt +14 -0
- data/doc/guides/source/actioncontroller_basics/params.txt +93 -0
- data/doc/guides/source/actioncontroller_basics/request_response_objects.txt +43 -0
- data/doc/guides/source/actioncontroller_basics/rescue.txt +67 -0
- data/doc/guides/source/actioncontroller_basics/session.txt +187 -0
- data/doc/guides/source/actioncontroller_basics/streaming.txt +91 -0
- data/doc/guides/source/actioncontroller_basics/verification.txt +40 -0
- data/doc/guides/source/active_record_basics.txt +181 -0
- data/doc/guides/source/activerecord_validations_callbacks.txt +404 -0
- data/doc/guides/source/association_basics.txt +1840 -0
- data/doc/guides/source/authors.txt +39 -0
- data/doc/guides/source/benchmarking_and_profiling/appendix.txt +95 -0
- data/doc/guides/source/benchmarking_and_profiling/digging_deeper.txt +105 -0
- data/doc/guides/source/benchmarking_and_profiling/edge_rails_features.txt +185 -0
- data/doc/guides/source/benchmarking_and_profiling/gameplan.txt +27 -0
- data/doc/guides/source/benchmarking_and_profiling/index.txt +242 -0
- data/doc/guides/source/benchmarking_and_profiling/rubyprof.txt +179 -0
- data/doc/guides/source/benchmarking_and_profiling/statistics.txt +57 -0
- data/doc/guides/source/caching_with_rails.txt +367 -0
- data/doc/guides/source/command_line.txt +147 -0
- data/doc/guides/source/configuring.txt +225 -0
- data/doc/guides/source/creating_plugins/acts_as_yaffle.txt +191 -0
- data/doc/guides/source/creating_plugins/appendix.txt +46 -0
- data/doc/guides/source/creating_plugins/controllers.txt +59 -0
- data/doc/guides/source/creating_plugins/core_ext.txt +123 -0
- data/doc/guides/source/creating_plugins/custom_route.txt +69 -0
- data/doc/guides/source/creating_plugins/gem.txt +1 -0
- data/doc/guides/source/creating_plugins/generator_method.txt +89 -0
- data/doc/guides/source/creating_plugins/helpers.txt +51 -0
- data/doc/guides/source/creating_plugins/index.txt +52 -0
- data/doc/guides/source/creating_plugins/migration_generator.txt +156 -0
- data/doc/guides/source/creating_plugins/models.txt +76 -0
- data/doc/guides/source/creating_plugins/odds_and_ends.txt +69 -0
- data/doc/guides/source/creating_plugins/test_setup.txt +230 -0
- data/doc/guides/source/debugging_rails_applications.txt +733 -0
- data/doc/guides/source/finders.txt +668 -0
- data/doc/guides/source/form_helpers.txt +345 -0
- data/doc/guides/source/getting_started_with_rails.txt +1256 -0
- data/doc/guides/source/images/belongs_to.png +0 -0
- data/doc/guides/source/images/bullet.gif +0 -0
- data/doc/guides/source/images/csrf.png +0 -0
- data/doc/guides/source/images/habtm.png +0 -0
- data/doc/guides/source/images/has_many.png +0 -0
- data/doc/guides/source/images/has_many_through.png +0 -0
- data/doc/guides/source/images/has_one.png +0 -0
- data/doc/guides/source/images/has_one_through.png +0 -0
- data/doc/guides/source/images/header_backdrop.png +0 -0
- data/doc/guides/source/images/icons/README +5 -0
- data/doc/guides/source/images/icons/callouts/1.png +0 -0
- data/doc/guides/source/images/icons/callouts/10.png +0 -0
- data/doc/guides/source/images/icons/callouts/11.png +0 -0
- data/doc/guides/source/images/icons/callouts/12.png +0 -0
- data/doc/guides/source/images/icons/callouts/13.png +0 -0
- data/doc/guides/source/images/icons/callouts/14.png +0 -0
- data/doc/guides/source/images/icons/callouts/15.png +0 -0
- data/doc/guides/source/images/icons/callouts/2.png +0 -0
- data/doc/guides/source/images/icons/callouts/3.png +0 -0
- data/doc/guides/source/images/icons/callouts/4.png +0 -0
- data/doc/guides/source/images/icons/callouts/5.png +0 -0
- data/doc/guides/source/images/icons/callouts/6.png +0 -0
- data/doc/guides/source/images/icons/callouts/7.png +0 -0
- data/doc/guides/source/images/icons/callouts/8.png +0 -0
- data/doc/guides/source/images/icons/callouts/9.png +0 -0
- data/doc/guides/source/images/icons/caution.png +0 -0
- data/doc/guides/source/images/icons/example.png +0 -0
- data/doc/guides/source/images/icons/home.png +0 -0
- data/doc/guides/source/images/icons/important.png +0 -0
- data/doc/guides/source/images/icons/next.png +0 -0
- data/doc/guides/source/images/icons/note.png +0 -0
- data/doc/guides/source/images/icons/prev.png +0 -0
- data/doc/guides/source/images/icons/tip.png +0 -0
- data/doc/guides/source/images/icons/up.png +0 -0
- data/doc/guides/source/images/icons/warning.png +0 -0
- data/doc/guides/source/images/polymorphic.png +0 -0
- data/doc/guides/source/images/rails_logo_remix.gif +0 -0
- data/doc/guides/source/images/ruby_on_rails_by_mike_rundle2.gif +0 -0
- data/doc/guides/source/images/session_fixation.png +0 -0
- data/doc/guides/source/index.txt +118 -0
- data/doc/guides/source/layouts_and_rendering.txt +982 -0
- data/doc/guides/source/migrations/anatomy_of_a_migration.txt +85 -0
- data/doc/guides/source/migrations/changelog.txt +5 -0
- data/doc/guides/source/migrations/creating_a_migration.txt +109 -0
- data/doc/guides/source/migrations/foreign_keys.txt +8 -0
- data/doc/guides/source/migrations/index.txt +22 -0
- data/doc/guides/source/migrations/rakeing_around.txt +111 -0
- data/doc/guides/source/migrations/scheming.txt +47 -0
- data/doc/guides/source/migrations/using_models_in_migrations.txt +46 -0
- data/doc/guides/source/migrations/writing_a_migration.txt +159 -0
- data/doc/guides/source/routing_outside_in.txt +986 -0
- data/doc/guides/source/security.txt +984 -0
- data/doc/guides/source/stylesheets/base.css +358 -0
- data/doc/guides/source/stylesheets/forms.css +35 -0
- data/doc/guides/source/stylesheets/more.css +82 -0
- data/doc/guides/source/templates/guides.html.erb +97 -0
- data/doc/guides/source/templates/inline.css +165 -0
- data/doc/guides/source/testing_rails_applications.txt +995 -0
- data/environments/boot.rb +2 -2
- data/environments/environment.rb +9 -1
- data/environments/production.rb +3 -1
- data/helpers/performance_test.rb +9 -0
- data/html/500.html +4 -1
- data/html/javascripts/controls.js +72 -72
- data/html/javascripts/dragdrop.js +165 -164
- data/html/javascripts/effects.js +173 -165
- data/html/javascripts/prototype.js +362 -267
- data/lib/commands/console.rb +13 -0
- data/lib/commands/dbconsole.rb +2 -2
- data/lib/commands/ncgi/listener +2 -2
- data/lib/commands/ncgi/tracker +2 -2
- data/lib/commands/plugin.rb +41 -24
- data/lib/commands/process/spawner.rb +4 -4
- data/lib/commands/runner.rb +1 -1
- data/lib/commands/server.rb +11 -1
- data/lib/commands/servers/thin.rb +25 -0
- data/lib/fcgi_handler.rb +1 -3
- data/lib/initializer.rb +162 -39
- data/lib/performance_test_help.rb +5 -0
- data/lib/rails/gem_builder.rb +3 -3
- data/lib/rails/gem_dependency.rb +155 -33
- data/lib/rails/mongrel_server/commands.rb +1 -1
- data/lib/rails/plugin.rb +10 -2
- data/lib/rails/rack.rb +6 -0
- data/lib/rails/rack/logger.rb +28 -0
- data/lib/rails/rack/static.rb +35 -0
- data/lib/rails/vendor_gem_source_index.rb +140 -0
- data/lib/rails/version.rb +1 -1
- data/lib/rails_generator/commands.rb +10 -16
- data/lib/rails_generator/generated_attribute.rb +4 -0
- data/lib/rails_generator/generators/applications/app/app_generator.rb +9 -3
- data/lib/rails_generator/generators/components/controller/controller_generator.rb +1 -1
- data/lib/rails_generator/generators/components/controller/templates/functional_test.rb +1 -1
- data/lib/rails_generator/generators/components/integration_test/integration_test_generator.rb +1 -1
- data/lib/rails_generator/generators/components/integration_test/templates/integration_test.rb +2 -2
- data/lib/rails_generator/generators/components/mailer/mailer_generator.rb +1 -1
- data/lib/rails_generator/generators/components/mailer/templates/unit_test.rb +2 -3
- data/lib/rails_generator/generators/components/model/model_generator.rb +1 -1
- data/lib/rails_generator/generators/components/model/templates/model.rb +3 -0
- data/lib/rails_generator/generators/components/model/templates/unit_test.rb +1 -1
- data/lib/rails_generator/generators/components/observer/observer_generator.rb +1 -1
- data/lib/rails_generator/generators/components/observer/templates/unit_test.rb +1 -1
- data/lib/rails_generator/generators/components/performance_test/USAGE +8 -0
- data/lib/rails_generator/generators/components/performance_test/performance_test_generator.rb +16 -0
- data/lib/rails_generator/generators/components/performance_test/templates/performance_test.rb +9 -0
- data/lib/rails_generator/generators/components/plugin/plugin_generator.rb +11 -11
- data/lib/rails_generator/generators/components/plugin/templates/Rakefile +1 -0
- data/lib/rails_generator/generators/components/plugin/templates/test_helper.rb +3 -0
- data/lib/rails_generator/generators/components/plugin/templates/unit_test.rb +4 -4
- data/lib/rails_generator/generators/components/resource/resource_generator.rb +2 -2
- data/lib/rails_generator/generators/components/resource/templates/functional_test.rb +1 -1
- data/lib/rails_generator/generators/components/scaffold/USAGE +9 -5
- data/lib/rails_generator/generators/components/scaffold/scaffold_generator.rb +10 -3
- data/lib/rails_generator/generators/components/scaffold/templates/functional_test.rb +7 -7
- data/lib/rails_generator/scripts.rb +1 -1
- data/lib/rails_generator/scripts/destroy.rb +6 -7
- data/lib/rails_generator/secret_key_generator.rb +5 -147
- data/lib/tasks/annotations.rake +8 -11
- data/lib/tasks/databases.rake +42 -15
- data/lib/tasks/documentation.rake +15 -0
- data/lib/tasks/framework.rake +5 -1
- data/lib/tasks/gems.rake +24 -8
- data/lib/tasks/misc.rake +3 -4
- data/lib/tasks/testing.rake +25 -4
- data/lib/test_help.rb +1 -2
- data/lib/webrick_server.rb +2 -11
- metadata +165 -9
@@ -0,0 +1,1090 @@
|
|
1
|
+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
2
|
+
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
|
3
|
+
<head>
|
4
|
+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
5
|
+
<title>Rails Finders</title>
|
6
|
+
<!--[if lt IE 8]>
|
7
|
+
<script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
|
8
|
+
<![endif]-->
|
9
|
+
<link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
|
10
|
+
<link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
|
11
|
+
<link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
|
12
|
+
<style type="text/css">
|
13
|
+
div#container {
|
14
|
+
max-width: 900px;
|
15
|
+
padding-bottom: 3em;
|
16
|
+
}
|
17
|
+
|
18
|
+
div#content {
|
19
|
+
margin-left: 200px;
|
20
|
+
}
|
21
|
+
|
22
|
+
div#container.notoc {
|
23
|
+
max-width: 600px;
|
24
|
+
}
|
25
|
+
|
26
|
+
.notoc div#content {
|
27
|
+
margin-left: 0;
|
28
|
+
}
|
29
|
+
|
30
|
+
pre {
|
31
|
+
line-height: 1.4em;
|
32
|
+
}
|
33
|
+
|
34
|
+
#content p tt {
|
35
|
+
background: #eeeeee;
|
36
|
+
border: solid 1px #cccccc;
|
37
|
+
padding: 3px;
|
38
|
+
}
|
39
|
+
|
40
|
+
dt {
|
41
|
+
font-weight: bold;
|
42
|
+
}
|
43
|
+
|
44
|
+
#content dt tt {
|
45
|
+
font-size: 10pt;
|
46
|
+
}
|
47
|
+
|
48
|
+
dd {
|
49
|
+
margin-left: 3em;
|
50
|
+
}
|
51
|
+
|
52
|
+
#content dt tt, #content pre tt {
|
53
|
+
background: none;
|
54
|
+
padding: 0;
|
55
|
+
border: 0;
|
56
|
+
}
|
57
|
+
|
58
|
+
#content .olist ol {
|
59
|
+
margin-left: 2em;
|
60
|
+
}
|
61
|
+
|
62
|
+
#header {
|
63
|
+
position: relative;
|
64
|
+
max-width: 840px;
|
65
|
+
margin-left: auto;
|
66
|
+
margin-right: auto;
|
67
|
+
}
|
68
|
+
|
69
|
+
#header.notoc {
|
70
|
+
max-width: 580px;
|
71
|
+
}
|
72
|
+
|
73
|
+
#logo {
|
74
|
+
position: absolute;
|
75
|
+
left: 10px;
|
76
|
+
top: 10px;
|
77
|
+
width: 110px;
|
78
|
+
height: 140px;
|
79
|
+
}
|
80
|
+
|
81
|
+
div#header h1#site_title {
|
82
|
+
background: url('images/ruby_on_rails_by_mike_rundle2.gif') top left no-repeat;
|
83
|
+
position: absolute;
|
84
|
+
width: 392px;
|
85
|
+
height: 55px;
|
86
|
+
left: 145px;
|
87
|
+
top: 20px;
|
88
|
+
margin: 0;
|
89
|
+
padding: 0;
|
90
|
+
}
|
91
|
+
|
92
|
+
#site_title span {
|
93
|
+
display: none;
|
94
|
+
}
|
95
|
+
|
96
|
+
#site_title_tagline {
|
97
|
+
display: none;
|
98
|
+
}
|
99
|
+
|
100
|
+
ul#navMain {
|
101
|
+
position: absolute;
|
102
|
+
margin: 0;
|
103
|
+
padding: 0;
|
104
|
+
top: 97px;
|
105
|
+
left: 145px;
|
106
|
+
}
|
107
|
+
|
108
|
+
.left-floaty, .right-floaty {
|
109
|
+
padding: 15px;
|
110
|
+
}
|
111
|
+
|
112
|
+
.admonitionblock,
|
113
|
+
.tableblock {
|
114
|
+
margin-left: 1em;
|
115
|
+
margin-right: 1em;
|
116
|
+
margin-top: 0.25em;
|
117
|
+
margin-bottom: 1em;
|
118
|
+
}
|
119
|
+
|
120
|
+
.admonitionblock .icon {
|
121
|
+
padding-right: 8px;
|
122
|
+
}
|
123
|
+
|
124
|
+
.admonitionblock .content {
|
125
|
+
border: solid 1px #ffda78;
|
126
|
+
background: #fffebd;
|
127
|
+
padding: 10px;
|
128
|
+
padding-top: 8px;
|
129
|
+
padding-bottom: 8px;
|
130
|
+
}
|
131
|
+
|
132
|
+
.admonitionblock .title {
|
133
|
+
font-size: 140%;
|
134
|
+
margin-bottom: 0.5em;
|
135
|
+
}
|
136
|
+
|
137
|
+
.tableblock table {
|
138
|
+
border: solid 1px #aaaaff;
|
139
|
+
background: #f0f0ff;
|
140
|
+
}
|
141
|
+
|
142
|
+
.tableblock th {
|
143
|
+
background: #e0e0e0;
|
144
|
+
}
|
145
|
+
|
146
|
+
.tableblock th,
|
147
|
+
.tableblock td {
|
148
|
+
padding: 3px;
|
149
|
+
padding-left: 5px;
|
150
|
+
padding-right: 5px;
|
151
|
+
}
|
152
|
+
|
153
|
+
.sidebarblock {
|
154
|
+
margin-top: 0.25em;
|
155
|
+
margin: 1em;
|
156
|
+
border: solid 1px #ccccbb;
|
157
|
+
padding: 8px;
|
158
|
+
background: #ffffe0;
|
159
|
+
}
|
160
|
+
|
161
|
+
.sidebarblock .sidebar-title {
|
162
|
+
font-size: 140%;
|
163
|
+
font-weight: 600;
|
164
|
+
margin-bottom: 0.3em;
|
165
|
+
}
|
166
|
+
|
167
|
+
.sidebarblock .sidebar-content > .para:last-child > p {
|
168
|
+
margin-bottom: 0;
|
169
|
+
}
|
170
|
+
|
171
|
+
.sidebarblock .sidebar-title a {
|
172
|
+
text-decoration: none;
|
173
|
+
}
|
174
|
+
|
175
|
+
.sidebarblock .sidebar-title a:hover {
|
176
|
+
text-decoration: underline;
|
177
|
+
}
|
178
|
+
|
179
|
+
</style>
|
180
|
+
</head>
|
181
|
+
<body>
|
182
|
+
<div id="header" >
|
183
|
+
<div id="logo">
|
184
|
+
<a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
|
185
|
+
</div>
|
186
|
+
|
187
|
+
<h1 id="site_title"><span>Ruby on Rails</span></h1>
|
188
|
+
<h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
|
189
|
+
|
190
|
+
<ul id="navMain">
|
191
|
+
<li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
|
192
|
+
<li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
|
193
|
+
</ul>
|
194
|
+
</div>
|
195
|
+
|
196
|
+
<div id="container">
|
197
|
+
|
198
|
+
<div id="sidebar">
|
199
|
+
<h2>Chapters</h2>
|
200
|
+
<ol>
|
201
|
+
<li>
|
202
|
+
<a href="#_the_sample_models">The Sample Models</a>
|
203
|
+
</li>
|
204
|
+
<li>
|
205
|
+
<a href="#_database_agnostic">Database Agnostic</a>
|
206
|
+
</li>
|
207
|
+
<li>
|
208
|
+
<a href="#_ids_first_last_and_all">IDs, First, Last and All</a>
|
209
|
+
</li>
|
210
|
+
<li>
|
211
|
+
<a href="#_conditions">Conditions</a>
|
212
|
+
<ul>
|
213
|
+
|
214
|
+
<li><a href="#_pure_string_conditions">Pure String Conditions</a></li>
|
215
|
+
|
216
|
+
<li><a href="#_array_conditions">Array Conditions</a></li>
|
217
|
+
|
218
|
+
<li><a href="#_hash_conditions">Hash Conditions</a></li>
|
219
|
+
|
220
|
+
</ul>
|
221
|
+
</li>
|
222
|
+
<li>
|
223
|
+
<a href="#_ordering">Ordering</a>
|
224
|
+
</li>
|
225
|
+
<li>
|
226
|
+
<a href="#_selecting_certain_fields">Selecting Certain Fields</a>
|
227
|
+
</li>
|
228
|
+
<li>
|
229
|
+
<a href="#_limit_amp_offset">Limit & Offset</a>
|
230
|
+
</li>
|
231
|
+
<li>
|
232
|
+
<a href="#_group">Group</a>
|
233
|
+
</li>
|
234
|
+
<li>
|
235
|
+
<a href="#_read_only">Read Only</a>
|
236
|
+
</li>
|
237
|
+
<li>
|
238
|
+
<a href="#_lock">Lock</a>
|
239
|
+
</li>
|
240
|
+
<li>
|
241
|
+
<a href="#_making_it_all_work_together">Making It All Work Together</a>
|
242
|
+
</li>
|
243
|
+
<li>
|
244
|
+
<a href="#_eager_loading">Eager Loading</a>
|
245
|
+
</li>
|
246
|
+
<li>
|
247
|
+
<a href="#_dynamic_finders">Dynamic finders</a>
|
248
|
+
</li>
|
249
|
+
<li>
|
250
|
+
<a href="#_finding_by_sql">Finding By SQL</a>
|
251
|
+
</li>
|
252
|
+
<li>
|
253
|
+
<a href="#_tt_select_all_tt"><tt>select_all</tt></a>
|
254
|
+
</li>
|
255
|
+
<li>
|
256
|
+
<a href="#_working_with_associations">Working with Associations</a>
|
257
|
+
</li>
|
258
|
+
<li>
|
259
|
+
<a href="#_named_scopes">Named Scopes</a>
|
260
|
+
<ul>
|
261
|
+
|
262
|
+
<li><a href="#_simple_named_scopes">Simple Named Scopes</a></li>
|
263
|
+
|
264
|
+
<li><a href="#_combining_named_scopes">Combining Named Scopes</a></li>
|
265
|
+
|
266
|
+
<li><a href="#_runtime_evaluation_of_named_scope_conditions">Runtime Evaluation of Named Scope Conditions</a></li>
|
267
|
+
|
268
|
+
<li><a href="#_named_scopes_with_multiple_models">Named Scopes with Multiple Models</a></li>
|
269
|
+
|
270
|
+
<li><a href="#_arguments_to_named_scopes">Arguments to Named Scopes</a></li>
|
271
|
+
|
272
|
+
<li><a href="#_anonymous_scopes">Anonymous Scopes</a></li>
|
273
|
+
|
274
|
+
</ul>
|
275
|
+
</li>
|
276
|
+
<li>
|
277
|
+
<a href="#_existence_of_objects">Existence of Objects</a>
|
278
|
+
</li>
|
279
|
+
<li>
|
280
|
+
<a href="#_calculations">Calculations</a>
|
281
|
+
<ul>
|
282
|
+
|
283
|
+
<li><a href="#_count">Count</a></li>
|
284
|
+
|
285
|
+
<li><a href="#_average">Average</a></li>
|
286
|
+
|
287
|
+
<li><a href="#_minimum">Minimum</a></li>
|
288
|
+
|
289
|
+
<li><a href="#_maximum">Maximum</a></li>
|
290
|
+
|
291
|
+
<li><a href="#_sum">Sum</a></li>
|
292
|
+
|
293
|
+
</ul>
|
294
|
+
</li>
|
295
|
+
<li>
|
296
|
+
<a href="#_credits">Credits</a>
|
297
|
+
</li>
|
298
|
+
<li>
|
299
|
+
<a href="#_changelog">Changelog</a>
|
300
|
+
</li>
|
301
|
+
</ol>
|
302
|
+
</div>
|
303
|
+
|
304
|
+
<div id="content">
|
305
|
+
<h1>Rails Finders</h1>
|
306
|
+
<div id="preamble">
|
307
|
+
<div class="sectionbody">
|
308
|
+
<div class="para"><p>This guide covers the <tt>find</tt> method defined in <tt>ActiveRecord::Base</tt>, as well as other ways of finding particular instances of your models. By using this guide, you will be able to:</p></div>
|
309
|
+
<div class="ilist"><ul>
|
310
|
+
<li>
|
311
|
+
<p>
|
312
|
+
Find records using a variety of methods and conditions
|
313
|
+
</p>
|
314
|
+
</li>
|
315
|
+
<li>
|
316
|
+
<p>
|
317
|
+
Specify the order, retrieved attributes, grouping, and other properties of the found records
|
318
|
+
</p>
|
319
|
+
</li>
|
320
|
+
<li>
|
321
|
+
<p>
|
322
|
+
Use eager loading to cut down on the number of database queries in your application
|
323
|
+
</p>
|
324
|
+
</li>
|
325
|
+
<li>
|
326
|
+
<p>
|
327
|
+
Use dynamic finders
|
328
|
+
</p>
|
329
|
+
</li>
|
330
|
+
<li>
|
331
|
+
<p>
|
332
|
+
Create named scopes to add custom finding behavior to your models
|
333
|
+
</p>
|
334
|
+
</li>
|
335
|
+
<li>
|
336
|
+
<p>
|
337
|
+
Check for the existence of particular records
|
338
|
+
</p>
|
339
|
+
</li>
|
340
|
+
<li>
|
341
|
+
<p>
|
342
|
+
Perform aggregate calculations on Active Record models
|
343
|
+
</p>
|
344
|
+
</li>
|
345
|
+
</ul></div>
|
346
|
+
<div class="para"><p>If you're used to using raw SQL to find database records, you'll find that there are generally better ways to carry out the same operations in Rails. Active Record insulates you from the need to use SQL in most cases.</p></div>
|
347
|
+
</div>
|
348
|
+
</div>
|
349
|
+
<h2 id="_the_sample_models">1. The Sample Models</h2>
|
350
|
+
<div class="sectionbody">
|
351
|
+
<div class="para"><p>This guide demonstrates finding using the following models:</p></div>
|
352
|
+
<div class="listingblock">
|
353
|
+
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
354
|
+
by Lorenzo Bettini
|
355
|
+
http://www.lorenzobettini.it
|
356
|
+
http://www.gnu.org/software/src-highlite -->
|
357
|
+
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Client <span style="color: #990000"><</span> ActiveRecord<span style="color: #990000">::</span>Base
|
358
|
+
has_one <span style="color: #990000">:</span>address
|
359
|
+
has_one <span style="color: #990000">:</span>mailing_address
|
360
|
+
has_many <span style="color: #990000">:</span>orders
|
361
|
+
has_and_belongs_to_many <span style="color: #990000">:</span>roles
|
362
|
+
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
363
|
+
|
364
|
+
<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Address <span style="color: #990000"><</span> ActiveRecord<span style="color: #990000">::</span>Base
|
365
|
+
belongs_to <span style="color: #990000">:</span>client
|
366
|
+
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
367
|
+
|
368
|
+
<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> MailingAddress <span style="color: #990000"><</span> Address
|
369
|
+
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
370
|
+
|
371
|
+
<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Order <span style="color: #990000"><</span> ActiveRecord<span style="color: #990000">::</span>Base
|
372
|
+
belongs_to <span style="color: #990000">:</span>client<span style="color: #990000">,</span> <span style="color: #990000">:</span>counter_cache <span style="color: #990000">=></span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
|
373
|
+
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
374
|
+
|
375
|
+
<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Role <span style="color: #990000"><</span> ActiveRecord<span style="color: #990000">::</span>Base
|
376
|
+
has_and_belongs_to_many <span style="color: #990000">:</span>clients
|
377
|
+
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
378
|
+
</tt></pre></div></div>
|
379
|
+
</div>
|
380
|
+
<h2 id="_database_agnostic">2. Database Agnostic</h2>
|
381
|
+
<div class="sectionbody">
|
382
|
+
<div class="para"><p>Active Record will perform queries on the database for you and is compatible with most database systems (MySQL, PostgreSQL and SQLite to name a few). Regardless of which database system you're using, the Active Record method format will always be the same.</p></div>
|
383
|
+
</div>
|
384
|
+
<h2 id="_ids_first_last_and_all">3. IDs, First, Last and All</h2>
|
385
|
+
<div class="sectionbody">
|
386
|
+
<div class="para"><p><tt>ActiveRecord::Base</tt> has methods defined on it to make interacting with your database and the tables within it much, much easier. For finding records, the key method is <tt>find</tt>. This method allows you to pass arguments into it to perform certain queries on your database without the need of SQL. If you wanted to find the record with the id of 1, you could type <tt>Client.find(1)</tt> which would execute this query on your database:</p></div>
|
387
|
+
<div class="listingblock">
|
388
|
+
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
389
|
+
by Lorenzo Bettini
|
390
|
+
http://www.lorenzobettini.it
|
391
|
+
http://www.gnu.org/software/src-highlite -->
|
392
|
+
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> <span style="color: #990000">+</span>clients<span style="color: #990000">+</span> <span style="font-weight: bold"><span style="color: #0000FF">WHERE</span></span> <span style="color: #990000">(+</span>clients<span style="color: #990000">+.+</span>id<span style="color: #990000">+</span> <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">)</span>
|
393
|
+
</tt></pre></div></div>
|
394
|
+
<div class="admonitionblock">
|
395
|
+
<table><tr>
|
396
|
+
<td class="icon">
|
397
|
+
<img src="./images/icons/note.png" alt="Note" />
|
398
|
+
</td>
|
399
|
+
<td class="content">Because this is a standard table created from a migration in Rail, the primary key is defaulted to <em>id</em>. If you have specified a different primary key in your migrations, this is what Rails will find on when you call the find method, not the id column.</td>
|
400
|
+
</tr></table>
|
401
|
+
</div>
|
402
|
+
<div class="para"><p>If you wanted to find clients with id 1 or 2, you call <tt>Client.find([1,2])</tt> or <tt>Client.find(1,2)</tt> and then this will be executed as:</p></div>
|
403
|
+
<div class="listingblock">
|
404
|
+
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
405
|
+
by Lorenzo Bettini
|
406
|
+
http://www.lorenzobettini.it
|
407
|
+
http://www.gnu.org/software/src-highlite -->
|
408
|
+
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> <span style="color: #990000">+</span>clients<span style="color: #990000">+</span> <span style="font-weight: bold"><span style="color: #0000FF">WHERE</span></span> <span style="color: #990000">(+</span>clients<span style="color: #990000">+.+</span>id<span style="color: #990000">+</span> <span style="font-weight: bold"><span style="color: #0000FF">IN</span></span> <span style="color: #990000">(</span><span style="color: #993399">1</span><span style="color: #990000">,</span><span style="color: #993399">2</span><span style="color: #990000">))</span>
|
409
|
+
</tt></pre></div></div>
|
410
|
+
<div class="listingblock">
|
411
|
+
<div class="content">
|
412
|
+
<pre><tt>>> Client.find(1,2)
|
413
|
+
=> [#<Client id: 1, name: => "Ryan", locked: false, orders_count: 2,
|
414
|
+
created_at: "2008-09-28 15:38:50", updated_at: "2008-09-28 15:38:50">,
|
415
|
+
#<Client id: 2, name: => "Michael", locked: false, orders_count: 3,
|
416
|
+
created_at: "2008-09-28 13:12:40", updated_at: "2008-09-28 13:12:40">]</tt></pre>
|
417
|
+
</div></div>
|
418
|
+
<div class="para"><p>Note that if you pass in a list of numbers that the result will be returned as an array, not as a single <tt>Client</tt> object.</p></div>
|
419
|
+
<div class="admonitionblock">
|
420
|
+
<table><tr>
|
421
|
+
<td class="icon">
|
422
|
+
<img src="./images/icons/note.png" alt="Note" />
|
423
|
+
</td>
|
424
|
+
<td class="content">If <tt>find(id)</tt> or <tt>find([id1, id2])</tt> fails to find any records, it will raise a <tt>RecordNotFound</tt> exception.</td>
|
425
|
+
</tr></table>
|
426
|
+
</div>
|
427
|
+
<div class="para"><p>If you wanted to find the first client you would simply type <tt>Client.first</tt> and that would find the first client created in your clients table:</p></div>
|
428
|
+
<div class="listingblock">
|
429
|
+
<div class="content">
|
430
|
+
<pre><tt>>> Client.first
|
431
|
+
=> #<Client id: 1, name: => "Ryan", locked: false, orders_count: 2,
|
432
|
+
created_at: "2008-09-28 15:38:50", updated_at: "2008-09-28 15:38:50"></tt></pre>
|
433
|
+
</div></div>
|
434
|
+
<div class="para"><p>If you were running script/server you might see the following output:</p></div>
|
435
|
+
<div class="listingblock">
|
436
|
+
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
437
|
+
by Lorenzo Bettini
|
438
|
+
http://www.lorenzobettini.it
|
439
|
+
http://www.gnu.org/software/src-highlite -->
|
440
|
+
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> clients <span style="font-weight: bold"><span style="color: #0000FF">LIMIT</span></span> <span style="color: #993399">1</span>
|
441
|
+
</tt></pre></div></div>
|
442
|
+
<div class="para"><p>Indicating the query that Rails has performed on your database.</p></div>
|
443
|
+
<div class="para"><p>To find the last client you would simply type <tt>Client.find(:last)</tt> and that would find the last client created in your clients table:</p></div>
|
444
|
+
<div class="listingblock">
|
445
|
+
<div class="content">
|
446
|
+
<pre><tt>>> Client.find(:last)
|
447
|
+
=> #<Client id: 2, name: => "Michael", locked: false, orders_count: 3,
|
448
|
+
created_at: "2008-09-28 13:12:40", updated_at: "2008-09-28 13:12:40"></tt></pre>
|
449
|
+
</div></div>
|
450
|
+
<div class="listingblock">
|
451
|
+
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
452
|
+
by Lorenzo Bettini
|
453
|
+
http://www.lorenzobettini.it
|
454
|
+
http://www.gnu.org/software/src-highlite -->
|
455
|
+
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> clients <span style="font-weight: bold"><span style="color: #0000FF">ORDER</span></span> <span style="font-weight: bold"><span style="color: #0000FF">BY</span></span> clients<span style="color: #990000">.</span>id <span style="font-weight: bold"><span style="color: #0000FF">DESC</span></span> <span style="font-weight: bold"><span style="color: #0000FF">LIMIT</span></span> <span style="color: #993399">1</span>
|
456
|
+
</tt></pre></div></div>
|
457
|
+
<div class="para"><p>To find all the clients you would simply type <tt>Client.all</tt> and that would find all the clients in your clients table:</p></div>
|
458
|
+
<div class="listingblock">
|
459
|
+
<div class="content">
|
460
|
+
<pre><tt>>> Client.all
|
461
|
+
=> [#<Client id: 1, name: => "Ryan", locked: false, orders_count: 2,
|
462
|
+
created_at: "2008-09-28 15:38:50", updated_at: "2008-09-28 15:38:50">,
|
463
|
+
#<Client id: 2, name: => "Michael", locked: false, orders_count: 3,
|
464
|
+
created_at: "2008-09-28 13:12:40", updated_at: "2008-09-28 13:12:40">]</tt></pre>
|
465
|
+
</div></div>
|
466
|
+
<div class="para"><p>As alternatives to calling <tt>Client.first</tt>, <tt>Client.last</tt>, and <tt>Client.all</tt>, you can use the class methods <tt>Client.first</tt>, <tt>Client.last</tt>, and <tt>Client.all</tt> instead. <tt>Client.first</tt>, <tt>Client.last</tt> and <tt>Client.all</tt> just call their longer counterparts: <tt>Client.find(:first)</tt>, <tt>Client.find(:last)</tt> and <tt>Client.find(:all)</tt> respectively.</p></div>
|
467
|
+
<div class="para"><p>Be aware that <tt>Client.first</tt>/<tt>Client.find(:first)</tt> and <tt>Client.last</tt>/<tt>Client.find(:last)</tt> will both return a single object, where as <tt>Client.all</tt>/<tt>Client.find(:all)</tt> will return an array of Client objects, just as passing in an array of ids to find will do also.</p></div>
|
468
|
+
</div>
|
469
|
+
<h2 id="_conditions">4. Conditions</h2>
|
470
|
+
<div class="sectionbody">
|
471
|
+
<div class="para"><p>The <tt>find</tt> method allows you to specify conditions to limit the records returned. You can specify conditions as a string, array, or hash.</p></div>
|
472
|
+
<h3 id="_pure_string_conditions">4.1. Pure String Conditions</h3>
|
473
|
+
<div class="para"><p>If you'd like to add conditions to your find, you could just specify them in there, just like <tt>Client.first(:conditions ⇒ "orders_count = <em>2</em>")</tt>. This will find all clients where the <tt>orders_count</tt> field's value is 2.</p></div>
|
474
|
+
<div class="admonitionblock">
|
475
|
+
<table><tr>
|
476
|
+
<td class="icon">
|
477
|
+
<img src="./images/icons/warning.png" alt="Warning" />
|
478
|
+
</td>
|
479
|
+
<td class="content">Building your own conditions as pure strings can leave you vulnerable to SQL injection exploits. For example, <tt>Client.first(:conditions ⇒ "name LIKE <em>%#{params[:name]}%</em>")</tt> is not safe. See the next section for the preferred way to handle conditions using an array.</td>
|
480
|
+
</tr></table>
|
481
|
+
</div>
|
482
|
+
<h3 id="_array_conditions">4.2. Array Conditions</h3>
|
483
|
+
<div class="para"><p>Now what if that number could vary, say as a parameter from somewhere, or perhaps from the user's level status somewhere? The find then becomes something like <tt>Client.first(:conditions ⇒ ["orders_count = ?", params[:orders]])</tt>. 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. If you want to specify two conditions, you can do it like <tt>Client.first(:conditions ⇒ ["orders_count = ? AND locked = ?", params[:orders], false])</tt>. In this example, the first question mark will be replaced with the value in params orders and the second will be replaced with true and this will find the first record in the table that has <em>2</em> as its value for the orders_count field and <em>false</em> for its locked field.</p></div>
|
484
|
+
<div class="para"><p>The reason for doing code like:</p></div>
|
485
|
+
<div class="listingblock">
|
486
|
+
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
487
|
+
by Lorenzo Bettini
|
488
|
+
http://www.lorenzobettini.it
|
489
|
+
http://www.gnu.org/software/src-highlite -->
|
490
|
+
<pre><tt><span style="color: #990000">+</span>Client<span style="color: #990000">.</span>first<span style="color: #990000">(:</span>conditions <span style="color: #990000">=></span> <span style="color: #990000">[</span><span style="color: #FF0000">"orders_count = ?"</span><span style="color: #990000">,</span> params<span style="color: #990000">[:</span>orders<span style="color: #990000">]])+</span>
|
491
|
+
</tt></pre></div></div>
|
492
|
+
<div class="para"><p>instead of:</p></div>
|
493
|
+
<div class="listingblock">
|
494
|
+
<div class="content">
|
495
|
+
<pre><tt>+Client.first(:conditions => "orders_count = #{params[:orders]}")+</tt></pre>
|
496
|
+
</div></div>
|
497
|
+
<div class="para"><p>is because of parameter safety. Putting the variable directly into the conditions string will pass the variable to the database <strong>as-is</strong>. This means that it will be an unescaped variable directly from a user who may have malicious intent. If you do this, you put your entire database at risk because once a user finds out he or she can exploit your database they can do just about anything to it. Never ever put your parameters directly inside the conditions string.</p></div>
|
498
|
+
<div class="admonitionblock">
|
499
|
+
<table><tr>
|
500
|
+
<td class="icon">
|
501
|
+
<img src="./images/icons/tip.png" alt="Tip" />
|
502
|
+
</td>
|
503
|
+
<td class="content">For more information on the dangers of SQL injection, see the <a href="../security.html#_sql_injection">Ruby on Rails Security Guide</a>.</td>
|
504
|
+
</tr></table>
|
505
|
+
</div>
|
506
|
+
<div class="para"><p>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:</p></div>
|
507
|
+
<div class="listingblock">
|
508
|
+
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
509
|
+
by Lorenzo Bettini
|
510
|
+
http://www.lorenzobettini.it
|
511
|
+
http://www.gnu.org/software/src-highlite -->
|
512
|
+
<pre><tt>Client<span style="color: #990000">.</span>all<span style="color: #990000">(:</span>conditions <span style="color: #990000">=></span> <span style="color: #990000">[</span><span style="color: #FF0000">"created_at IN (?)"</span><span style="color: #990000">,</span>
|
513
|
+
<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>start_date<span style="color: #990000">].</span>to_date<span style="color: #990000">)..(</span>params<span style="color: #990000">[:</span>end_date<span style="color: #990000">].</span>to_date<span style="color: #990000">)])</span>
|
514
|
+
</tt></pre></div></div>
|
515
|
+
<div class="para"><p>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.</p></div>
|
516
|
+
<div class="listingblock">
|
517
|
+
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
518
|
+
by Lorenzo Bettini
|
519
|
+
http://www.lorenzobettini.it
|
520
|
+
http://www.gnu.org/software/src-highlite -->
|
521
|
+
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> <span style="color: #990000">+</span>users<span style="color: #990000">+</span> <span style="font-weight: bold"><span style="color: #0000FF">WHERE</span></span> <span style="color: #990000">(</span>created_at <span style="font-weight: bold"><span style="color: #0000FF">IN</span></span>
|
522
|
+
<span style="color: #990000">(</span><span style="color: #FF0000">'2007-12-31'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-01'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-02'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-03'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-04'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-05'</span><span style="color: #990000">,</span>
|
523
|
+
<span style="color: #FF0000">'2008-01-06'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-07'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-08'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-09'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-10'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-11'</span><span style="color: #990000">,</span>
|
524
|
+
<span style="color: #FF0000">'2008-01-12'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-13'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-14'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-15'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-16'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-17'</span><span style="color: #990000">,</span>
|
525
|
+
<span style="color: #FF0000">'2008-01-18'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-19'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-20'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-21'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-22'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-23'</span><span style="color: #990000">,...</span>
|
526
|
+
‘<span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">15</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">16</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">17</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">18</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">19</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">20</span><span style="color: #FF0000">',</span>
|
527
|
+
<span style="color: #FF0000"> '</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">21</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">22</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">23</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">24</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">25</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">26</span><span style="color: #FF0000">',</span>
|
528
|
+
<span style="color: #FF0000"> '</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">27</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">28</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">29</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">30</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">31</span><span style="color: #FF0000">'))</span>
|
529
|
+
</tt></pre></div></div>
|
530
|
+
<div class="para"><p>Things can get <strong>really</strong> messy if you pass in time objects as it will attempt to compare your field to <strong>every second</strong> in that range:</p></div>
|
531
|
+
<div class="listingblock">
|
532
|
+
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
533
|
+
by Lorenzo Bettini
|
534
|
+
http://www.lorenzobettini.it
|
535
|
+
http://www.gnu.org/software/src-highlite -->
|
536
|
+
<pre><tt>Client<span style="color: #990000">.</span>all<span style="color: #990000">(:</span>conditions <span style="color: #990000">=></span> <span style="color: #990000">[</span><span style="color: #FF0000">"created_at IN (?)"</span><span style="color: #990000">,</span>
|
537
|
+
<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>start_date<span style="color: #990000">].</span>to_date<span style="color: #990000">.</span>to_time<span style="color: #990000">)..(</span>params<span style="color: #990000">[:</span>end_date<span style="color: #990000">].</span>to_date<span style="color: #990000">.</span>to_time<span style="color: #990000">)])</span>
|
538
|
+
</tt></pre></div></div>
|
539
|
+
<div class="listingblock">
|
540
|
+
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
541
|
+
by Lorenzo Bettini
|
542
|
+
http://www.lorenzobettini.it
|
543
|
+
http://www.gnu.org/software/src-highlite -->
|
544
|
+
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> <span style="color: #990000">+</span>users<span style="color: #990000">+</span> <span style="font-weight: bold"><span style="color: #0000FF">WHERE</span></span> <span style="color: #990000">(</span>created_at <span style="font-weight: bold"><span style="color: #0000FF">IN</span></span>
|
545
|
+
<span style="color: #990000">(</span><span style="color: #FF0000">'2007-12-01 00:00:00'</span><span style="color: #990000">,</span> <span style="color: #FF0000">'2007-12-01 00:00:01'</span> <span style="color: #990000">...</span>
|
546
|
+
<span style="color: #FF0000">'2007-12-01 23:59:59'</span><span style="color: #990000">,</span> <span style="color: #FF0000">'2007-12-02 00:00:00'</span><span style="color: #990000">))</span>
|
547
|
+
</tt></pre></div></div>
|
548
|
+
<div class="para"><p>This could possibly cause your database server to raise an unexpected error, for example MySQL will throw back this error:</p></div>
|
549
|
+
<div class="listingblock">
|
550
|
+
<div class="content">
|
551
|
+
<pre><tt>Got a packet bigger than 'max_allowed_packet' bytes: _query_</tt></pre>
|
552
|
+
</div></div>
|
553
|
+
<div class="para"><p>Where <em>query</em> is the actual query used to get that error.</p></div>
|
554
|
+
<div class="para"><p>In this example it would be better to use greater-than and less-than operators in SQL, like so:</p></div>
|
555
|
+
<div class="listingblock">
|
556
|
+
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
557
|
+
by Lorenzo Bettini
|
558
|
+
http://www.lorenzobettini.it
|
559
|
+
http://www.gnu.org/software/src-highlite -->
|
560
|
+
<pre><tt>Client<span style="color: #990000">.</span>all<span style="color: #990000">(:</span>conditions <span style="color: #990000">=></span>
|
561
|
+
<span style="color: #990000">[</span><span style="color: #FF0000">"created_at > ? AND created_at < ?"</span><span style="color: #990000">,</span> params<span style="color: #990000">[:</span>start_date<span style="color: #990000">],</span> params<span style="color: #990000">[:</span>end_date<span style="color: #990000">]])</span>
|
562
|
+
</tt></pre></div></div>
|
563
|
+
<div class="para"><p>You can also use the greater-than-or-equal-to and less-than-or-equal-to like this:</p></div>
|
564
|
+
<div class="listingblock">
|
565
|
+
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
566
|
+
by Lorenzo Bettini
|
567
|
+
http://www.lorenzobettini.it
|
568
|
+
http://www.gnu.org/software/src-highlite -->
|
569
|
+
<pre><tt>Client<span style="color: #990000">.</span>all<span style="color: #990000">(:</span>conditions <span style="color: #990000">=></span>
|
570
|
+
<span style="color: #990000">[</span><span style="color: #FF0000">"created_at >= ? AND created_at <= ?"</span><span style="color: #990000">,</span> params<span style="color: #990000">[:</span>start_date<span style="color: #990000">],</span> params<span style="color: #990000">[:</span>end_date<span style="color: #990000">]])</span>
|
571
|
+
</tt></pre></div></div>
|
572
|
+
<div class="para"><p>Just like in Ruby.</p></div>
|
573
|
+
<h3 id="_hash_conditions">4.3. Hash Conditions</h3>
|
574
|
+
<div class="para"><p>Similar to the array style of params you can also specify keys in your conditions:</p></div>
|
575
|
+
<div class="listingblock">
|
576
|
+
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
577
|
+
by Lorenzo Bettini
|
578
|
+
http://www.lorenzobettini.it
|
579
|
+
http://www.gnu.org/software/src-highlite -->
|
580
|
+
<pre><tt>Client<span style="color: #990000">.</span>all<span style="color: #990000">(:</span>conditions <span style="color: #990000">=></span>
|
581
|
+
<span style="color: #990000">[</span><span style="color: #FF0000">"created_at >= :start_date AND created_at <= :end_date"</span><span style="color: #990000">,</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>start_date <span style="color: #990000">=></span> params<span style="color: #990000">[:</span>start_date<span style="color: #990000">],</span> <span style="color: #990000">:</span>end_date <span style="color: #990000">=></span> params<span style="color: #990000">[:</span>end_date<span style="color: #990000">]</span> <span style="color: #FF0000">}</span><span style="color: #990000">])</span>
|
582
|
+
</tt></pre></div></div>
|
583
|
+
<div class="para"><p>This makes for clearer readability if you have a large number of variable conditions.</p></div>
|
584
|
+
</div>
|
585
|
+
<h2 id="_ordering">5. Ordering</h2>
|
586
|
+
<div class="sectionbody">
|
587
|
+
<div class="para"><p>If you're getting a set of records and want to force an order, you can use <tt>Client.all(:order ⇒ "created_at")</tt> which by default will sort the records by ascending order. If you'd like to order it in descending order, just tell it to do that using <tt>Client.all(:order ⇒ "created_at desc")</tt></p></div>
|
588
|
+
</div>
|
589
|
+
<h2 id="_selecting_certain_fields">6. Selecting Certain Fields</h2>
|
590
|
+
<div class="sectionbody">
|
591
|
+
<div class="para"><p>To select certain fields, you can use the select option like this: <tt>Client.first(:select ⇒ "viewable_by, locked")</tt>. This select option does not use an array of fields, but rather requires you to type SQL-like code. The above code will execute <tt>SELECT viewable_by, locked FROM clients LIMIT 0,1</tt> on your database.</p></div>
|
592
|
+
</div>
|
593
|
+
<h2 id="_limit_amp_offset">7. Limit & Offset</h2>
|
594
|
+
<div class="sectionbody">
|
595
|
+
<div class="para"><p>If you want to limit the amount of records to a certain subset of all the records retrieved you usually use limit for this, sometimes coupled with offset. Limit is the maximum number of records that will be retrieved from a query, and offset is the number of records it will start reading from from the first record of the set. Take this code for example:</p></div>
|
596
|
+
<div class="listingblock">
|
597
|
+
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
598
|
+
by Lorenzo Bettini
|
599
|
+
http://www.lorenzobettini.it
|
600
|
+
http://www.gnu.org/software/src-highlite -->
|
601
|
+
<pre><tt>Client<span style="color: #990000">.</span>all<span style="color: #990000">(:</span>limit <span style="color: #990000">=></span> <span style="color: #993399">5</span><span style="color: #990000">)</span>
|
602
|
+
</tt></pre></div></div>
|
603
|
+
<div class="para"><p>This code will return a maximum of 5 clients and because it specifies no offset it will return the first 5 clients in the table. The SQL it executes will look like this:</p></div>
|
604
|
+
<div class="listingblock">
|
605
|
+
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
606
|
+
by Lorenzo Bettini
|
607
|
+
http://www.lorenzobettini.it
|
608
|
+
http://www.gnu.org/software/src-highlite -->
|
609
|
+
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> clients <span style="font-weight: bold"><span style="color: #0000FF">LIMIT</span></span> <span style="color: #993399">5</span>
|
610
|
+
</tt></pre></div></div>
|
611
|
+
<div class="listingblock">
|
612
|
+
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
613
|
+
by Lorenzo Bettini
|
614
|
+
http://www.lorenzobettini.it
|
615
|
+
http://www.gnu.org/software/src-highlite -->
|
616
|
+
<pre><tt>Client<span style="color: #990000">.</span>all<span style="color: #990000">(:</span>limit <span style="color: #990000">=></span> <span style="color: #993399">5</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>offset <span style="color: #990000">=></span> <span style="color: #993399">5</span><span style="color: #990000">)</span>
|
617
|
+
</tt></pre></div></div>
|
618
|
+
<div class="para"><p>This code will return a maximum of 5 clients and because it specifies an offset this time, it will return these records starting from the 5th client in the clients table. The SQL looks like:</p></div>
|
619
|
+
<div class="listingblock">
|
620
|
+
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
621
|
+
by Lorenzo Bettini
|
622
|
+
http://www.lorenzobettini.it
|
623
|
+
http://www.gnu.org/software/src-highlite -->
|
624
|
+
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> clients <span style="font-weight: bold"><span style="color: #0000FF">LIMIT</span></span> <span style="color: #993399">5</span><span style="color: #990000">,</span> <span style="color: #993399">5</span>
|
625
|
+
</tt></pre></div></div>
|
626
|
+
</div>
|
627
|
+
<h2 id="_group">8. Group</h2>
|
628
|
+
<div class="sectionbody">
|
629
|
+
<div class="para"><p>The group option for find is useful, for example, if you want to find a collection of the dates orders were created on. You could use the option in this context:</p></div>
|
630
|
+
<div class="listingblock">
|
631
|
+
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
632
|
+
by Lorenzo Bettini
|
633
|
+
http://www.lorenzobettini.it
|
634
|
+
http://www.gnu.org/software/src-highlite -->
|
635
|
+
<pre><tt>Order<span style="color: #990000">.</span>all<span style="color: #990000">(:</span>group <span style="color: #990000">=></span> <span style="color: #FF0000">"date(created_at)"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>order <span style="color: #990000">=></span> <span style="color: #FF0000">"created_at"</span><span style="color: #990000">)</span>
|
636
|
+
</tt></pre></div></div>
|
637
|
+
<div class="para"><p>And this will give you a single <tt>Order</tt> object for each date where there are orders in the database.</p></div>
|
638
|
+
<div class="para"><p>The SQL that would be executed would be something like this:</p></div>
|
639
|
+
<div class="listingblock">
|
640
|
+
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
641
|
+
by Lorenzo Bettini
|
642
|
+
http://www.lorenzobettini.it
|
643
|
+
http://www.gnu.org/software/src-highlite -->
|
644
|
+
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> <span style="color: #990000">+</span>orders<span style="color: #990000">+</span> <span style="font-weight: bold"><span style="color: #0000FF">GROUP</span></span> <span style="font-weight: bold"><span style="color: #0000FF">BY</span></span> <span style="color: #009900">date</span><span style="color: #990000">(</span>created_at<span style="color: #990000">)</span>
|
645
|
+
</tt></pre></div></div>
|
646
|
+
</div>
|
647
|
+
<h2 id="_read_only">9. Read Only</h2>
|
648
|
+
<div class="sectionbody">
|
649
|
+
<div class="para"><p>Readonly is a find option that you can set in order to make that instance of the record read-only. Any attempt to alter or destroy the record will not succeed, raising an <tt>Active Record::ReadOnlyRecord</tt> exception. To set this option, specify it like this:</p></div>
|
650
|
+
<div class="listingblock">
|
651
|
+
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
652
|
+
by Lorenzo Bettini
|
653
|
+
http://www.lorenzobettini.it
|
654
|
+
http://www.gnu.org/software/src-highlite -->
|
655
|
+
<pre><tt>Client<span style="color: #990000">.</span>first<span style="color: #990000">(:</span>readonly <span style="color: #990000">=></span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">)</span>
|
656
|
+
</tt></pre></div></div>
|
657
|
+
<div class="para"><p>If you assign this record to a variable <tt>client</tt>, calling the following code will raise an <tt>ActiveRecord::ReadOnlyRecord</tt> exception:</p></div>
|
658
|
+
<div class="listingblock">
|
659
|
+
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
660
|
+
by Lorenzo Bettini
|
661
|
+
http://www.lorenzobettini.it
|
662
|
+
http://www.gnu.org/software/src-highlite -->
|
663
|
+
<pre><tt>client <span style="color: #990000">=</span> Client<span style="color: #990000">.</span>first<span style="color: #990000">(:</span>readonly <span style="color: #990000">=></span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">)</span>
|
664
|
+
client<span style="color: #990000">.</span>locked <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span>
|
665
|
+
client<span style="color: #990000">.</span>save
|
666
|
+
</tt></pre></div></div>
|
667
|
+
</div>
|
668
|
+
<h2 id="_lock">10. Lock</h2>
|
669
|
+
<div class="sectionbody">
|
670
|
+
<div class="para"><p>If you're wanting to stop race conditions for a specific record (for example, you're incrementing a single field for a record, potentially from multiple simultaneous connections) you can use the lock option to ensure that the record is updated correctly. For safety, you should use this inside a transaction.</p></div>
|
671
|
+
<div class="listingblock">
|
672
|
+
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
673
|
+
by Lorenzo Bettini
|
674
|
+
http://www.lorenzobettini.it
|
675
|
+
http://www.gnu.org/software/src-highlite -->
|
676
|
+
<pre><tt>Topic<span style="color: #990000">.</span>transaction <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
|
677
|
+
t <span style="color: #990000">=</span> Topic<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">],</span> <span style="color: #990000">:</span>lock <span style="color: #990000">=></span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">)</span>
|
678
|
+
t<span style="color: #990000">.</span>increment!<span style="color: #990000">(:</span>views<span style="color: #990000">)</span>
|
679
|
+
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
680
|
+
</tt></pre></div></div>
|
681
|
+
</div>
|
682
|
+
<h2 id="_making_it_all_work_together">11. Making It All Work Together</h2>
|
683
|
+
<div class="sectionbody">
|
684
|
+
<div class="para"><p>You can chain these options together in no particular order as Active Record will write the correct SQL for you. If you specify two instances of the same options inside the find statement Active Record will use the latter.</p></div>
|
685
|
+
</div>
|
686
|
+
<h2 id="_eager_loading">12. Eager Loading</h2>
|
687
|
+
<div class="sectionbody">
|
688
|
+
<div class="para"><p>Eager loading is loading associated records along with any number of records in as few queries as possible. For example, if you wanted to load all the addresses associated with all the clients in a single query you could use <tt>Client.all(:include ⇒ :address)</tt>. If you wanted to include both the address and mailing address for the client you would use +Client.find(:all, :include ⇒ [:address, :mailing_address]). Include will first find the client records and then load the associated address records. Running script/server in one window, and executing the code through script/console in another window, the output should look similar to this:</p></div>
|
689
|
+
<div class="listingblock">
|
690
|
+
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
691
|
+
by Lorenzo Bettini
|
692
|
+
http://www.lorenzobettini.it
|
693
|
+
http://www.gnu.org/software/src-highlite -->
|
694
|
+
<pre><tt>Client <span style="font-weight: bold"><span style="color: #0000FF">Load</span></span> <span style="color: #990000">(</span><span style="color: #993399">0.000383</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> clients
|
695
|
+
Address <span style="font-weight: bold"><span style="color: #0000FF">Load</span></span> <span style="color: #990000">(</span><span style="color: #993399">0.119770</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> addresses<span style="color: #990000">.*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> addresses
|
696
|
+
<span style="font-weight: bold"><span style="color: #0000FF">WHERE</span></span> <span style="color: #990000">(</span>addresses<span style="color: #990000">.</span>client_id <span style="font-weight: bold"><span style="color: #0000FF">IN</span></span> <span style="color: #990000">(</span><span style="color: #993399">13</span><span style="color: #990000">,</span><span style="color: #993399">14</span><span style="color: #990000">))</span>
|
697
|
+
MailingAddress <span style="font-weight: bold"><span style="color: #0000FF">Load</span></span> <span style="color: #990000">(</span><span style="color: #993399">0.001985</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> mailing_addresses<span style="color: #990000">.*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span>
|
698
|
+
mailing_addresses <span style="font-weight: bold"><span style="color: #0000FF">WHERE</span></span> <span style="color: #990000">(</span>mailing_addresses<span style="color: #990000">.</span>client_id <span style="font-weight: bold"><span style="color: #0000FF">IN</span></span> <span style="color: #990000">(</span><span style="color: #993399">13</span><span style="color: #990000">,</span><span style="color: #993399">14</span><span style="color: #990000">))</span>
|
699
|
+
</tt></pre></div></div>
|
700
|
+
<div class="para"><p>The numbers <tt>13</tt> and <tt>14</tt> in the above SQL are the ids of the clients gathered from the <tt>Client.all</tt> query. Rails will then run a query to gather all the addresses and mailing addresses that have a client_id of 13 or 14. Although this is done in 3 queries, this is more efficient than not eager loading because without eager loading it would run a query for every time you called <tt>address</tt> or <tt>mailing_address</tt> on one of the objects in the clients array, which may lead to performance issues if you're loading a large number of records at once.</p></div>
|
701
|
+
<div class="para"><p>If you wanted to get all the addresses for a client in the same query you would do <tt>Client.all(:joins ⇒ :address)</tt> and you wanted to find the address and mailing address for that client you would do <tt>Client.all(:joins ⇒ [:address, :mailing_address])</tt>. This is more efficient because it does all the SQL in one query, as shown by this example:</p></div>
|
702
|
+
<div class="listingblock">
|
703
|
+
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
704
|
+
by Lorenzo Bettini
|
705
|
+
http://www.lorenzobettini.it
|
706
|
+
http://www.gnu.org/software/src-highlite -->
|
707
|
+
<pre><tt><span style="color: #990000">+</span>Client <span style="font-weight: bold"><span style="color: #0000FF">Load</span></span> <span style="color: #990000">(</span><span style="color: #993399">0.000455</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> clients<span style="color: #990000">.*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> clients <span style="font-weight: bold"><span style="color: #0000FF">INNER</span></span> <span style="font-weight: bold"><span style="color: #0000FF">JOIN</span></span> addresses
|
708
|
+
<span style="font-weight: bold"><span style="color: #0000FF">ON</span></span> addresses<span style="color: #990000">.</span>client_id <span style="color: #990000">=</span> client<span style="color: #990000">.</span>id <span style="font-weight: bold"><span style="color: #0000FF">INNER</span></span> <span style="font-weight: bold"><span style="color: #0000FF">JOIN</span></span> mailing_addresses <span style="font-weight: bold"><span style="color: #0000FF">ON</span></span>
|
709
|
+
mailing_addresses<span style="color: #990000">.</span>client_id <span style="color: #990000">=</span> client<span style="color: #990000">.</span>id
|
710
|
+
</tt></pre></div></div>
|
711
|
+
<div class="para"><p>This query is more efficent, but there's a gotcha: if you have a client who does not have an address or a mailing address they will not be returned in this query at all. If you have any association as an optional association, you may want to use include rather than joins. Alternatively, you can use a SQL join clause to specify exactly the join you need (Rails always assumes an inner join):</p></div>
|
712
|
+
<div class="listingblock">
|
713
|
+
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
714
|
+
by Lorenzo Bettini
|
715
|
+
http://www.lorenzobettini.it
|
716
|
+
http://www.gnu.org/software/src-highlite -->
|
717
|
+
<pre><tt>Client<span style="color: #990000">.</span>all<span style="color: #990000">(:</span>joins <span style="color: #990000">=></span> “LEFT OUTER JOIN addresses ON
|
718
|
+
client<span style="color: #990000">.</span>id <span style="color: #990000">=</span> addresses<span style="color: #990000">.</span>client_id LEFT OUTER JOIN mailing_addresses ON
|
719
|
+
client<span style="color: #990000">.</span>id <span style="color: #990000">=</span> mailing_addresses<span style="color: #990000">.</span>client_id”<span style="color: #990000">)</span>
|
720
|
+
</tt></pre></div></div>
|
721
|
+
<div class="para"><p>When using eager loading you can specify conditions for the columns of the tables inside the eager loading to get back a smaller subset. If, for example, you want to find a client and all their orders within the last two weeks you could use eager loading with conditions for this:</p></div>
|
722
|
+
<div class="listingblock">
|
723
|
+
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
724
|
+
by Lorenzo Bettini
|
725
|
+
http://www.lorenzobettini.it
|
726
|
+
http://www.gnu.org/software/src-highlite -->
|
727
|
+
<pre><tt>Client<span style="color: #990000">.</span>first<span style="color: #990000">(:</span><span style="font-weight: bold"><span style="color: #0000FF">include</span></span> <span style="color: #990000">=></span> <span style="color: #FF0000">"orders"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=></span>
|
728
|
+
<span style="color: #990000">[</span><span style="color: #FF0000">"orders.created_at >= ? AND orders.created_at <= ?"</span><span style="color: #990000">,</span> Time<span style="color: #990000">.</span>now <span style="color: #990000">-</span> <span style="color: #993399">2</span><span style="color: #990000">.</span>weeks<span style="color: #990000">,</span> Time<span style="color: #990000">.</span>now<span style="color: #990000">])</span>
|
729
|
+
</tt></pre></div></div>
|
730
|
+
</div>
|
731
|
+
<h2 id="_dynamic_finders">13. Dynamic finders</h2>
|
732
|
+
<div class="sectionbody">
|
733
|
+
<div class="para"><p>For every field (also known as an attribute) you define in your table, Active Record provides a finder method. If you have a field called <tt>name</tt> on your Client model for example, you get <tt>find_by_name</tt> and <tt>find_all_by_name</tt> for free from Active Record. If you have also have a <tt>locked</tt> field on the client model, you also get <tt>find_by_locked</tt> and <tt>find_all_by_locked</tt>. If you want to find both by name and locked, you can chain these finders together by simply typing <tt>and</tt> between the fields for example <tt>Client.find_by_name_and_locked(<em>Ryan</em>, true)</tt>. These finders are an excellent alternative to using the conditions option, mainly because it's shorter to type <tt>find_by_name(params[:name])</tt> than it is to type <tt>first(:conditions ⇒ ["name = ?", params[:name]])</tt>.</p></div>
|
734
|
+
<div class="para"><p>There's another set of dynamic finders that let you find or create/initialize objects if they aren't find. These work in a similar fashion to the other finders and can be used like <tt>find_or_create_by_name(params[:name])</tt>. Using this will firstly perform a find and then create if the find returns nil. The SQL looks like this for <tt>Client.find_or_create_by_name(<em>Ryan</em>)</tt>:</p></div>
|
735
|
+
<div class="listingblock">
|
736
|
+
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
737
|
+
by Lorenzo Bettini
|
738
|
+
http://www.lorenzobettini.it
|
739
|
+
http://www.gnu.org/software/src-highlite -->
|
740
|
+
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> <span style="color: #990000">+</span>clients<span style="color: #990000">+</span> <span style="font-weight: bold"><span style="color: #0000FF">WHERE</span></span> <span style="color: #990000">(+</span>clients<span style="color: #990000">+.+</span>name<span style="color: #990000">+</span> <span style="color: #990000">=</span> <span style="color: #FF0000">'Ryan'</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">LIMIT</span></span> <span style="color: #993399">1</span>
|
741
|
+
BEGIN
|
742
|
+
<span style="font-weight: bold"><span style="color: #0000FF">INSERT</span></span> <span style="font-weight: bold"><span style="color: #0000FF">INTO</span></span> <span style="color: #990000">+</span>clients<span style="color: #990000">+</span> <span style="color: #990000">(+</span>name<span style="color: #990000">+,</span> <span style="color: #990000">+</span>updated_at<span style="color: #990000">+,</span> <span style="color: #990000">+</span>created_at<span style="color: #990000">+,</span> <span style="color: #990000">+</span>orders_count<span style="color: #990000">+,</span> <span style="color: #990000">+</span>locked<span style="color: #990000">+)</span>
|
743
|
+
<span style="font-weight: bold"><span style="color: #0000FF">VALUES</span></span><span style="color: #990000">(</span><span style="color: #FF0000">'Ryan'</span><span style="color: #990000">,</span> <span style="color: #FF0000">'2008-09-28 15:39:12'</span><span style="color: #990000">,</span> <span style="color: #FF0000">'2008-09-28 15:39:12'</span><span style="color: #990000">,</span> <span style="color: #FF0000">'0'</span><span style="color: #990000">,</span> <span style="color: #FF0000">'0'</span><span style="color: #990000">)</span>
|
744
|
+
COMMIT
|
745
|
+
</tt></pre></div></div>
|
746
|
+
<div class="para"><p><tt>find_or_create</tt>'s sibling, <tt>find_or_initialize</tt>, will find an object and if it does not exist will call <tt>new</tt> with the parameters you passed in. For example:</p></div>
|
747
|
+
<div class="listingblock">
|
748
|
+
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
749
|
+
by Lorenzo Bettini
|
750
|
+
http://www.lorenzobettini.it
|
751
|
+
http://www.gnu.org/software/src-highlite -->
|
752
|
+
<pre><tt>client <span style="color: #990000">=</span> Client<span style="color: #990000">.</span>find_or_initialize_by_name<span style="color: #990000">(</span><span style="color: #FF0000">'Ryan'</span><span style="color: #990000">)</span>
|
753
|
+
</tt></pre></div></div>
|
754
|
+
<div class="para"><p>will either assign an existing client object with the name <em>Ryan</em> to the client local variable, or initialize new object similar to calling <tt>Client.new(:name ⇒ <em>Ryan</em>)</tt>. From here, you can modify other fields in client by calling the attribute setters on it: <tt>client.locked = true</tt> and when you want to write it to the database just call <tt>save</tt> on it.</p></div>
|
755
|
+
</div>
|
756
|
+
<h2 id="_finding_by_sql">14. Finding By SQL</h2>
|
757
|
+
<div class="sectionbody">
|
758
|
+
<div class="para"><p>If you'd like to use your own SQL to find records a table you can use <tt>find_by_sql</tt>. The <tt>find_by_sql</tt> method 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:</p></div>
|
759
|
+
<div class="listingblock">
|
760
|
+
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
761
|
+
by Lorenzo Bettini
|
762
|
+
http://www.lorenzobettini.it
|
763
|
+
http://www.gnu.org/software/src-highlite -->
|
764
|
+
<pre><tt>Client<span style="color: #990000">.</span>find_by_sql<span style="color: #990000">(</span><span style="color: #FF0000">"SELECT * FROM clients INNER JOIN orders ON clients.id = orders.client_id ORDER clients.created_at desc"</span><span style="color: #990000">)</span>
|
765
|
+
</tt></pre></div></div>
|
766
|
+
<div class="para"><p><tt>find_by_sql</tt> provides you with a simple way of making custom calls to the database and retrieving instantiated objects.</p></div>
|
767
|
+
</div>
|
768
|
+
<h2 id="_tt_select_all_tt">15. <tt>select_all</tt></h2>
|
769
|
+
<div class="sectionbody">
|
770
|
+
<div class="para"><p><tt>find_by_sql</tt> has a close relative called <tt>connection#select_all</tt>. <tt>select_all</tt> will retrieve objects from the database using custom SQL just like <tt>find_by_sql</tt> but will not instantiate them. Instead, you will get an array of hashes where each hash indicates a record.</p></div>
|
771
|
+
<div class="listingblock">
|
772
|
+
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
773
|
+
by Lorenzo Bettini
|
774
|
+
http://www.lorenzobettini.it
|
775
|
+
http://www.gnu.org/software/src-highlite -->
|
776
|
+
<pre><tt>Client<span style="color: #990000">.</span>connection<span style="color: #990000">.</span>select_all<span style="color: #990000">(</span><span style="color: #FF0000">"SELECT * FROM `clients` WHERE `id` = '1'"</span><span style="color: #990000">)</span>
|
777
|
+
</tt></pre></div></div>
|
778
|
+
</div>
|
779
|
+
<h2 id="_working_with_associations">16. Working with Associations</h2>
|
780
|
+
<div class="sectionbody">
|
781
|
+
<div class="para"><p>When you define a has_many association on a model you get the find method and dynamic finders also on that association. This is helpful for finding associated records within the scope of an existing record, for example finding all the orders for a client that have been sent and not received by doing something like <tt>Client.find(params[:id]).orders.find_by_sent_and_received(true, false)</tt>. Having this find method available on associations is extremely helpful when using nested controllers.</p></div>
|
782
|
+
</div>
|
783
|
+
<h2 id="_named_scopes">17. Named Scopes</h2>
|
784
|
+
<div class="sectionbody">
|
785
|
+
<div class="para"><p>Named scopes are another way to add custom finding behavior to the models in the application. Named scopes provide an object-oriented way to narrow the results of a query.</p></div>
|
786
|
+
<h3 id="_simple_named_scopes">17.1. Simple Named Scopes</h3>
|
787
|
+
<div class="para"><p>Suppose want to find all clients who are male. You could use this code:</p></div>
|
788
|
+
<div class="listingblock">
|
789
|
+
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
790
|
+
by Lorenzo Bettini
|
791
|
+
http://www.lorenzobettini.it
|
792
|
+
http://www.gnu.org/software/src-highlite -->
|
793
|
+
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Client <span style="color: #990000"><</span> ActiveRecord<span style="color: #990000">::</span>Base
|
794
|
+
named_scope <span style="color: #990000">:</span>males<span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=></span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>gender <span style="color: #990000">=></span> <span style="color: #FF0000">"male"</span> <span style="color: #FF0000">}</span>
|
795
|
+
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
796
|
+
</tt></pre></div></div>
|
797
|
+
<div class="para"><p>Then you could call <tt>Client.males.all</tt> to get all the clients who are male. Please note that if you do not specify the <tt>all</tt> on the end you will get a <tt>Scope</tt> object back, not a set of records which you do get back if you put the <tt>all</tt> on the end.</p></div>
|
798
|
+
<div class="para"><p>If you wanted to find all the clients who are active, you could use this:</p></div>
|
799
|
+
<div class="listingblock">
|
800
|
+
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
801
|
+
by Lorenzo Bettini
|
802
|
+
http://www.lorenzobettini.it
|
803
|
+
http://www.gnu.org/software/src-highlite -->
|
804
|
+
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Client <span style="color: #990000"><</span> ActiveRecord<span style="color: #990000">::</span>Base
|
805
|
+
named_scope <span style="color: #990000">:</span>active<span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=></span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>active <span style="color: #990000">=></span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span> <span style="color: #FF0000">}</span>
|
806
|
+
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
807
|
+
</tt></pre></div></div>
|
808
|
+
<div class="para"><p>You can call this new named_scope with <tt>Client.active.all</tt> and this will do the same query as if we just used <tt>Client.all(:conditions ⇒ ["active = ?", true])</tt>. Please be aware that the conditions syntax in named_scope and find is different and the two are not interchangeable. If you want to find the first client within this named scope you could do <tt>Client.active.first</tt>.</p></div>
|
809
|
+
<h3 id="_combining_named_scopes">17.2. Combining Named Scopes</h3>
|
810
|
+
<div class="para"><p>If you wanted to find all the clients who are active and male you can stack the named scopes like this:</p></div>
|
811
|
+
<div class="listingblock">
|
812
|
+
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
813
|
+
by Lorenzo Bettini
|
814
|
+
http://www.lorenzobettini.it
|
815
|
+
http://www.gnu.org/software/src-highlite -->
|
816
|
+
<pre><tt>Client<span style="color: #990000">.</span>males<span style="color: #990000">.</span>active<span style="color: #990000">.</span>all
|
817
|
+
</tt></pre></div></div>
|
818
|
+
<div class="para"><p>If you would then like to do a <tt>all</tt> on that scope, you can. Just like an association, named scopes allow you to call <tt>all</tt> on them:</p></div>
|
819
|
+
<div class="listingblock">
|
820
|
+
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
821
|
+
by Lorenzo Bettini
|
822
|
+
http://www.lorenzobettini.it
|
823
|
+
http://www.gnu.org/software/src-highlite -->
|
824
|
+
<pre><tt>Client<span style="color: #990000">.</span>males<span style="color: #990000">.</span>active<span style="color: #990000">.</span>all<span style="color: #990000">(:</span>conditions <span style="color: #990000">=></span> <span style="color: #990000">[</span><span style="color: #FF0000">"age > ?"</span><span style="color: #990000">,</span> params<span style="color: #990000">[:</span>age<span style="color: #990000">]])</span>
|
825
|
+
</tt></pre></div></div>
|
826
|
+
<h3 id="_runtime_evaluation_of_named_scope_conditions">17.3. Runtime Evaluation of Named Scope Conditions</h3>
|
827
|
+
<div class="para"><p>Consider the following code:</p></div>
|
828
|
+
<div class="listingblock">
|
829
|
+
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
830
|
+
by Lorenzo Bettini
|
831
|
+
http://www.lorenzobettini.it
|
832
|
+
http://www.gnu.org/software/src-highlite -->
|
833
|
+
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Client <span style="color: #990000"><</span> ActiveRecord<span style="color: #990000">::</span>Base
|
834
|
+
named_scope <span style="color: #990000">:</span>recent<span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=></span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>created_at <span style="color: #990000">></span> <span style="color: #993399">2</span><span style="color: #990000">.</span>weeks<span style="color: #990000">.</span>ago <span style="color: #FF0000">}</span>
|
835
|
+
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
836
|
+
</tt></pre></div></div>
|
837
|
+
<div class="para"><p>This looks like a standard named scope that defines a method called recent which gathers all records created any time between now and 2 weeks ago. That's correct for the first time the model is loaded but for any time after that, <tt>2.weeks.ago</tt> is set to that same value, so you will consistently get records from a certain date until your model is reloaded by something like your application restarting. The way to fix this is to put the code in a lambda block:</p></div>
|
838
|
+
<div class="listingblock">
|
839
|
+
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
840
|
+
by Lorenzo Bettini
|
841
|
+
http://www.lorenzobettini.it
|
842
|
+
http://www.gnu.org/software/src-highlite -->
|
843
|
+
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Client <span style="color: #990000"><</span> ActiveRecord<span style="color: #990000">::</span>Base
|
844
|
+
named_scope <span style="color: #990000">:</span>recent<span style="color: #990000">,</span> lambda <span style="color: #FF0000">{</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=></span> <span style="color: #990000">[</span><span style="color: #FF0000">"created_at > ?"</span><span style="color: #990000">,</span> <span style="color: #993399">2</span><span style="color: #990000">.</span>weeks<span style="color: #990000">.</span>ago<span style="color: #990000">]</span> <span style="color: #FF0000">}</span> <span style="color: #FF0000">}</span>
|
845
|
+
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
846
|
+
</tt></pre></div></div>
|
847
|
+
<div class="para"><p>And now every time the recent named scope is called, the code in the lambda block will be parsed, so you'll get actually 2 weeks ago from the code execution, not 2 weeks ago from the time the model was loaded.</p></div>
|
848
|
+
<h3 id="_named_scopes_with_multiple_models">17.4. Named Scopes with Multiple Models</h3>
|
849
|
+
<div class="para"><p>In a named scope you can use <tt>:include</tt> and <tt>:joins</tt> options just like in find.</p></div>
|
850
|
+
<div class="listingblock">
|
851
|
+
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
852
|
+
by Lorenzo Bettini
|
853
|
+
http://www.lorenzobettini.it
|
854
|
+
http://www.gnu.org/software/src-highlite -->
|
855
|
+
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Client <span style="color: #990000"><</span> ActiveRecord<span style="color: #990000">::</span>Base
|
856
|
+
named_scope <span style="color: #990000">:</span>active_within_2_weeks<span style="color: #990000">,</span> <span style="color: #990000">:</span>joins <span style="color: #990000">=></span> <span style="color: #990000">:</span>order<span style="color: #990000">,</span>
|
857
|
+
lambda <span style="color: #FF0000">{</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=></span> <span style="color: #990000">[</span><span style="color: #FF0000">"orders.created_at > ?"</span><span style="color: #990000">,</span> <span style="color: #993399">2</span><span style="color: #990000">.</span>weeks<span style="color: #990000">.</span>ago<span style="color: #990000">]</span> <span style="color: #FF0000">}</span> <span style="color: #FF0000">}</span>
|
858
|
+
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
859
|
+
</tt></pre></div></div>
|
860
|
+
<div class="para"><p>This method, called as <tt>Client.active_within_2_weeks.all</tt>, will return all clients who have placed orders in the past 2 weeks.</p></div>
|
861
|
+
<h3 id="_arguments_to_named_scopes">17.5. Arguments to Named Scopes</h3>
|
862
|
+
<div class="para"><p>If you want to pass a named scope a compulsory argument, just specify it as a block parameter like this:</p></div>
|
863
|
+
<div class="listingblock">
|
864
|
+
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
865
|
+
by Lorenzo Bettini
|
866
|
+
http://www.lorenzobettini.it
|
867
|
+
http://www.gnu.org/software/src-highlite -->
|
868
|
+
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Client <span style="color: #990000"><</span> ActiveRecord<span style="color: #990000">::</span>Base
|
869
|
+
named_scope <span style="color: #990000">:</span>recent<span style="color: #990000">,</span> lambda <span style="color: #FF0000">{</span> <span style="color: #990000">|</span>time<span style="color: #990000">|</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=></span> <span style="color: #990000">[</span><span style="color: #FF0000">"created_at > ?"</span><span style="color: #990000">,</span> time<span style="color: #990000">]</span> <span style="color: #FF0000">}</span> <span style="color: #FF0000">}</span>
|
870
|
+
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
871
|
+
</tt></pre></div></div>
|
872
|
+
<div class="para"><p>This will work if you call <tt>Client.recent(2.weeks.ago).all</tt> but not if you call <tt>Client.recent</tt>. If you want to add an optional argument for this, you have to use the splat operator as the block's parameter.</p></div>
|
873
|
+
<div class="listingblock">
|
874
|
+
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
875
|
+
by Lorenzo Bettini
|
876
|
+
http://www.lorenzobettini.it
|
877
|
+
http://www.gnu.org/software/src-highlite -->
|
878
|
+
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Client <span style="color: #990000"><</span> ActiveRecord<span style="color: #990000">::</span>Base
|
879
|
+
named_scope <span style="color: #990000">:</span>recent<span style="color: #990000">,</span> lambda <span style="color: #FF0000">{</span> <span style="color: #990000">|*</span>args<span style="color: #990000">|</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=></span> <span style="color: #990000">[</span><span style="color: #FF0000">"created_at > ?"</span><span style="color: #990000">,</span> args<span style="color: #990000">.</span>first <span style="color: #990000">||</span> <span style="color: #993399">2</span><span style="color: #990000">.</span>weeks<span style="color: #990000">.</span>ago<span style="color: #990000">]</span> <span style="color: #FF0000">}</span> <span style="color: #FF0000">}</span>
|
880
|
+
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
881
|
+
</tt></pre></div></div>
|
882
|
+
<div class="para"><p>This will work with <tt>Client.recent(2.weeks.ago).all</tt> and <tt>Client.recent.all</tt>, with the latter always returning records with a created_at date between right now and 2 weeks ago.</p></div>
|
883
|
+
<div class="para"><p>Remember that named scopes are stackable, so you will be able to do <tt>Client.recent(2.weeks.ago).unlocked.all</tt> to find all clients created between right now and 2 weeks ago and have their locked field set to false.</p></div>
|
884
|
+
<h3 id="_anonymous_scopes">17.6. Anonymous Scopes</h3>
|
885
|
+
<div class="para"><p>All Active Record models come with a named scope named <tt>scoped</tt>, which allows you to create anonymous scopes. For example:</p></div>
|
886
|
+
<div class="listingblock">
|
887
|
+
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
888
|
+
by Lorenzo Bettini
|
889
|
+
http://www.lorenzobettini.it
|
890
|
+
http://www.gnu.org/software/src-highlite -->
|
891
|
+
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Client <span style="color: #990000"><</span> ActiveRecord<span style="color: #990000">::</span>Base
|
892
|
+
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>recent
|
893
|
+
scoped <span style="color: #990000">:</span>conditions <span style="color: #990000">=></span> <span style="color: #990000">[</span><span style="color: #FF0000">"created_at > ?"</span><span style="color: #990000">,</span> <span style="color: #993399">2</span><span style="color: #990000">.</span>weeks<span style="color: #990000">.</span>ago<span style="color: #990000">]</span>
|
894
|
+
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
895
|
+
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
896
|
+
</tt></pre></div></div>
|
897
|
+
<div class="para"><p>Anonymous scopes are most useful to create scopes "on the fly":</p></div>
|
898
|
+
<div class="listingblock">
|
899
|
+
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
900
|
+
by Lorenzo Bettini
|
901
|
+
http://www.lorenzobettini.it
|
902
|
+
http://www.gnu.org/software/src-highlite -->
|
903
|
+
<pre><tt>Client<span style="color: #990000">.</span>scoped<span style="color: #990000">(:</span>conditions <span style="color: #990000">=></span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>gender <span style="color: #990000">=></span> <span style="color: #FF0000">"male"</span> <span style="color: #FF0000">}</span><span style="color: #990000">)</span>
|
904
|
+
</tt></pre></div></div>
|
905
|
+
<div class="para"><p>Just like named scopes, anonymous scopes can be stacked, either with other anonymous scopes or with regular named scopes.</p></div>
|
906
|
+
</div>
|
907
|
+
<h2 id="_existence_of_objects">18. Existence of Objects</h2>
|
908
|
+
<div class="sectionbody">
|
909
|
+
<div class="para"><p>If you simply want to check for the existence of the object there's a method called <tt>exists?</tt>. 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.</p></div>
|
910
|
+
<div class="listingblock">
|
911
|
+
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
912
|
+
by Lorenzo Bettini
|
913
|
+
http://www.lorenzobettini.it
|
914
|
+
http://www.gnu.org/software/src-highlite -->
|
915
|
+
<pre><tt>Client<span style="color: #990000">.</span>exists?<span style="color: #990000">(</span><span style="color: #993399">1</span><span style="color: #990000">)</span>
|
916
|
+
</tt></pre></div></div>
|
917
|
+
<div class="para"><p>The above code will check for the existence of a clients table record with the id of 1 and return true if it exists.</p></div>
|
918
|
+
<div class="listingblock">
|
919
|
+
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
920
|
+
by Lorenzo Bettini
|
921
|
+
http://www.lorenzobettini.it
|
922
|
+
http://www.gnu.org/software/src-highlite -->
|
923
|
+
<pre><tt>Client<span style="color: #990000">.</span>exists?<span style="color: #990000">(</span><span style="color: #993399">1</span><span style="color: #990000">,</span><span style="color: #993399">2</span><span style="color: #990000">,</span><span style="color: #993399">3</span><span style="color: #990000">)</span>
|
924
|
+
<span style="font-style: italic"><span style="color: #9A1900"># or</span></span>
|
925
|
+
Client<span style="color: #990000">.</span>exists?<span style="color: #990000">([</span><span style="color: #993399">1</span><span style="color: #990000">,</span><span style="color: #993399">2</span><span style="color: #990000">,</span><span style="color: #993399">3</span><span style="color: #990000">])</span>
|
926
|
+
</tt></pre></div></div>
|
927
|
+
<div class="para"><p>The <tt>exists?</tt> method 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.</p></div>
|
928
|
+
<div class="para"><p>Further more, <tt>exists</tt> takes a <tt>conditions</tt> option much like find:</p></div>
|
929
|
+
<div class="listingblock">
|
930
|
+
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
931
|
+
by Lorenzo Bettini
|
932
|
+
http://www.lorenzobettini.it
|
933
|
+
http://www.gnu.org/software/src-highlite -->
|
934
|
+
<pre><tt>Client<span style="color: #990000">.</span>exists?<span style="color: #990000">(:</span>conditions <span style="color: #990000">=></span> <span style="color: #FF0000">"first_name = 'Ryan'"</span><span style="color: #990000">)</span>
|
935
|
+
</tt></pre></div></div>
|
936
|
+
</div>
|
937
|
+
<h2 id="_calculations">19. Calculations</h2>
|
938
|
+
<div class="sectionbody">
|
939
|
+
<div class="para"><p>This section uses count as an example method in this preamble, but the options described apply to all sub-sections.</p></div>
|
940
|
+
<div class="para"><p><tt>count</tt> takes conditions much in the same way <tt>exists?</tt> does:</p></div>
|
941
|
+
<div class="listingblock">
|
942
|
+
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
943
|
+
by Lorenzo Bettini
|
944
|
+
http://www.lorenzobettini.it
|
945
|
+
http://www.gnu.org/software/src-highlite -->
|
946
|
+
<pre><tt>Client<span style="color: #990000">.</span>count<span style="color: #990000">(:</span>conditions <span style="color: #990000">=></span> <span style="color: #FF0000">"first_name = 'Ryan'"</span><span style="color: #990000">)</span>
|
947
|
+
</tt></pre></div></div>
|
948
|
+
<div class="para"><p>Which will execute:</p></div>
|
949
|
+
<div class="listingblock">
|
950
|
+
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
951
|
+
by Lorenzo Bettini
|
952
|
+
http://www.lorenzobettini.it
|
953
|
+
http://www.gnu.org/software/src-highlite -->
|
954
|
+
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> count<span style="color: #990000">(*)</span> <span style="font-weight: bold"><span style="color: #0000FF">AS</span></span> count_all <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> <span style="color: #990000">+</span>clients<span style="color: #990000">+</span> <span style="font-weight: bold"><span style="color: #0000FF">WHERE</span></span> <span style="color: #990000">(</span>first_name <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">)</span>
|
955
|
+
</tt></pre></div></div>
|
956
|
+
<div class="para"><p>You can also use <tt>include</tt> or <tt>joins</tt> for this to do something a little more complex:</p></div>
|
957
|
+
<div class="listingblock">
|
958
|
+
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
959
|
+
by Lorenzo Bettini
|
960
|
+
http://www.lorenzobettini.it
|
961
|
+
http://www.gnu.org/software/src-highlite -->
|
962
|
+
<pre><tt>Client<span style="color: #990000">.</span>count<span style="color: #990000">(:</span>conditions <span style="color: #990000">=></span> <span style="color: #FF0000">"clients.first_name = 'Ryan' AND orders.status = 'received'"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">include</span></span> <span style="color: #990000">=></span> <span style="color: #FF0000">"orders"</span><span style="color: #990000">)</span>
|
963
|
+
</tt></pre></div></div>
|
964
|
+
<div class="para"><p>Which will execute:</p></div>
|
965
|
+
<div class="listingblock">
|
966
|
+
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
967
|
+
by Lorenzo Bettini
|
968
|
+
http://www.lorenzobettini.it
|
969
|
+
http://www.gnu.org/software/src-highlite -->
|
970
|
+
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> count<span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">DISTINCT</span></span> <span style="color: #990000">+</span>clients<span style="color: #990000">+.</span>id<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">AS</span></span> count_all <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> <span style="color: #990000">+</span>clients<span style="color: #990000">+</span>
|
971
|
+
<span style="font-weight: bold"><span style="color: #0000FF">LEFT</span></span> <span style="font-weight: bold"><span style="color: #0000FF">OUTER</span></span> <span style="font-weight: bold"><span style="color: #0000FF">JOIN</span></span> <span style="color: #990000">+</span>orders<span style="color: #990000">+</span> <span style="font-weight: bold"><span style="color: #0000FF">ON</span></span> orders<span style="color: #990000">.</span>client_id <span style="color: #990000">=</span> client<span style="color: #990000">.</span>id <span style="font-weight: bold"><span style="color: #0000FF">WHERE</span></span>
|
972
|
+
<span style="color: #990000">(</span>clients<span style="color: #990000">.</span>first_name <span style="color: #990000">=</span> <span style="color: #FF0000">'name'</span> <span style="font-weight: bold"><span style="color: #0000FF">AND</span></span> orders<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #0000FF">status</span></span> <span style="color: #990000">=</span> <span style="color: #FF0000">'received'</span><span style="color: #990000">)</span>
|
973
|
+
</tt></pre></div></div>
|
974
|
+
<div class="para"><p>This code specifies <tt>clients.first_name</tt> just in case one of the join tables has a field also called <tt>first_name</tt> and it uses <tt>orders.status</tt> because that's the name of our join table.</p></div>
|
975
|
+
<h3 id="_count">19.1. Count</h3>
|
976
|
+
<div class="para"><p>If you want to see how many records are in your model's table you could call <tt>Client.count</tt> 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 <tt>Client.count(:age)</tt>.</p></div>
|
977
|
+
<div class="para"><p>For options, please see the parent section, Calculations.</p></div>
|
978
|
+
<h3 id="_average">19.2. Average</h3>
|
979
|
+
<div class="para"><p>If you want to see the average of a certain number in one of your tables you can call the <tt>average</tt> method on the class that relates to the table. This method call will look something like this:</p></div>
|
980
|
+
<div class="listingblock">
|
981
|
+
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
982
|
+
by Lorenzo Bettini
|
983
|
+
http://www.lorenzobettini.it
|
984
|
+
http://www.gnu.org/software/src-highlite -->
|
985
|
+
<pre><tt>Client<span style="color: #990000">.</span>average<span style="color: #990000">(</span><span style="color: #FF0000">"orders_count"</span><span style="color: #990000">)</span>
|
986
|
+
</tt></pre></div></div>
|
987
|
+
<div class="para"><p>This will return a number (possibly a floating point number such as 3.14159265) representing the average value in the field.</p></div>
|
988
|
+
<div class="para"><p>For options, please see the parent section, <a href="#_calculations">Calculations</a></p></div>
|
989
|
+
<h3 id="_minimum">19.3. Minimum</h3>
|
990
|
+
<div class="para"><p>If you want to find the minimum value of a field in your table you can call the <tt>minimum</tt> method on the class that relates to the table. This method call will look something like this:</p></div>
|
991
|
+
<div class="listingblock">
|
992
|
+
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
993
|
+
by Lorenzo Bettini
|
994
|
+
http://www.lorenzobettini.it
|
995
|
+
http://www.gnu.org/software/src-highlite -->
|
996
|
+
<pre><tt>Client<span style="color: #990000">.</span>minimum<span style="color: #990000">(</span><span style="color: #FF0000">"age"</span><span style="color: #990000">)</span>
|
997
|
+
</tt></pre></div></div>
|
998
|
+
<div class="para"><p>For options, please see the parent section, <a href="#_calculations">Calculations</a></p></div>
|
999
|
+
<h3 id="_maximum">19.4. Maximum</h3>
|
1000
|
+
<div class="para"><p>If you want to find the maximum value of a field in your table you can call the <tt>maximum</tt> method on the class that relates to the table. This method call will look something like this:</p></div>
|
1001
|
+
<div class="listingblock">
|
1002
|
+
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
1003
|
+
by Lorenzo Bettini
|
1004
|
+
http://www.lorenzobettini.it
|
1005
|
+
http://www.gnu.org/software/src-highlite -->
|
1006
|
+
<pre><tt>Client<span style="color: #990000">.</span>maximum<span style="color: #990000">(</span><span style="color: #FF0000">"age"</span><span style="color: #990000">)</span>
|
1007
|
+
</tt></pre></div></div>
|
1008
|
+
<div class="para"><p>For options, please see the parent section, <a href="#_calculations">Calculations</a></p></div>
|
1009
|
+
<h3 id="_sum">19.5. Sum</h3>
|
1010
|
+
<div class="para"><p>If you want to find the sum of a field for all records in your table you can call the <tt>sum</tt> method on the class that relates to the table. This method call will look something like this:</p></div>
|
1011
|
+
<div class="listingblock">
|
1012
|
+
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
1013
|
+
by Lorenzo Bettini
|
1014
|
+
http://www.lorenzobettini.it
|
1015
|
+
http://www.gnu.org/software/src-highlite -->
|
1016
|
+
<pre><tt>Client<span style="color: #990000">.</span>sum<span style="color: #990000">(</span><span style="color: #FF0000">"orders_count"</span><span style="color: #990000">)</span>
|
1017
|
+
</tt></pre></div></div>
|
1018
|
+
<div class="para"><p>For options, please see the parent section, <a href="#_calculations">Calculations</a></p></div>
|
1019
|
+
</div>
|
1020
|
+
<h2 id="_credits">20. Credits</h2>
|
1021
|
+
<div class="sectionbody">
|
1022
|
+
<div class="para"><p>Thanks to Ryan Bates for his awesome screencast on named scope #108. The information within the named scope section is intentionally similar to it, and without the cast may have not been possible.</p></div>
|
1023
|
+
<div class="para"><p>Thanks to Mike Gunderloy for his tips on creating this guide.</p></div>
|
1024
|
+
</div>
|
1025
|
+
<h2 id="_changelog">21. Changelog</h2>
|
1026
|
+
<div class="sectionbody">
|
1027
|
+
<div class="para"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/16">Lighthouse ticket</a></p></div>
|
1028
|
+
<div class="ilist"><ul>
|
1029
|
+
<li>
|
1030
|
+
<p>
|
1031
|
+
November 8, 2008: Editing pass by <a href="../authors.html#mgunderloy">Mike Gunderloy</a> . First release version.
|
1032
|
+
</p>
|
1033
|
+
</li>
|
1034
|
+
<li>
|
1035
|
+
<p>
|
1036
|
+
October 27, 2008: Added scoped section, added named params for conditions and added sub-section headers for conditions section by Ryan Bigg
|
1037
|
+
</p>
|
1038
|
+
</li>
|
1039
|
+
<li>
|
1040
|
+
<p>
|
1041
|
+
October 27, 2008: Fixed up all points specified in <a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/16-activerecord-finders#ticket-16-6">this comment</a> with an exception of the final point by Ryan Bigg
|
1042
|
+
</p>
|
1043
|
+
</li>
|
1044
|
+
<li>
|
1045
|
+
<p>
|
1046
|
+
October 26, 2008: Editing pass by <a href="../authors.html#mgunderloy">Mike Gunderloy</a> . First release version.
|
1047
|
+
</p>
|
1048
|
+
</li>
|
1049
|
+
<li>
|
1050
|
+
<p>
|
1051
|
+
October 22, 2008: Calculations complete, first complete draft by Ryan Bigg
|
1052
|
+
</p>
|
1053
|
+
</li>
|
1054
|
+
<li>
|
1055
|
+
<p>
|
1056
|
+
October 21, 2008: Extended named scope section by Ryan Bigg
|
1057
|
+
</p>
|
1058
|
+
</li>
|
1059
|
+
<li>
|
1060
|
+
<p>
|
1061
|
+
October 9, 2008: Lock, count, cleanup by Ryan Bigg
|
1062
|
+
</p>
|
1063
|
+
</li>
|
1064
|
+
<li>
|
1065
|
+
<p>
|
1066
|
+
October 6, 2008: Eager loading by Ryan Bigg
|
1067
|
+
</p>
|
1068
|
+
</li>
|
1069
|
+
<li>
|
1070
|
+
<p>
|
1071
|
+
October 5, 2008: Covered conditions by Ryan Bigg
|
1072
|
+
</p>
|
1073
|
+
</li>
|
1074
|
+
<li>
|
1075
|
+
<p>
|
1076
|
+
October 1, 2008: Covered limit/offset, formatting changes by Ryan Bigg
|
1077
|
+
</p>
|
1078
|
+
</li>
|
1079
|
+
<li>
|
1080
|
+
<p>
|
1081
|
+
September 28, 2008: Covered first/last/all by Ryan Bigg
|
1082
|
+
</p>
|
1083
|
+
</li>
|
1084
|
+
</ul></div>
|
1085
|
+
</div>
|
1086
|
+
|
1087
|
+
</div>
|
1088
|
+
</div>
|
1089
|
+
</body>
|
1090
|
+
</html>
|