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.

Files changed (221) hide show
  1. data/CHANGELOG +27 -3
  2. data/Rakefile +70 -10
  3. data/bin/about +0 -0
  4. data/bin/console +0 -0
  5. data/bin/destroy +0 -0
  6. data/bin/generate +0 -0
  7. data/bin/performance/benchmarker +0 -0
  8. data/bin/performance/profiler +0 -0
  9. data/bin/performance/request +0 -0
  10. data/bin/plugin +0 -0
  11. data/bin/process/inspector +0 -0
  12. data/bin/process/reaper +0 -0
  13. data/bin/process/spawner +0 -0
  14. data/bin/runner +0 -0
  15. data/bin/server +0 -0
  16. data/config.ru +17 -0
  17. data/configs/apache.conf +0 -0
  18. data/configs/databases/ibm_db.yml +62 -0
  19. data/configs/databases/mysql.yml +3 -0
  20. data/configs/databases/postgresql.yml +3 -0
  21. data/configs/databases/sqlite2.yml +3 -0
  22. data/configs/databases/sqlite3.yml +3 -0
  23. data/configs/locales/en.yml +5 -0
  24. data/dispatches/dispatch.fcgi +1 -1
  25. data/dispatches/dispatch.rb +2 -2
  26. data/dispatches/gateway.cgi +2 -2
  27. data/doc/README_FOR_APP +5 -2
  28. data/doc/guides/html/2_2_release_notes.html +1185 -0
  29. data/doc/guides/html/actioncontroller_basics.html +1270 -0
  30. data/doc/guides/html/activerecord_validations_callbacks.html +749 -0
  31. data/doc/guides/html/association_basics.html +2585 -0
  32. data/doc/guides/html/authors.html +240 -0
  33. data/doc/guides/html/benchmarking_and_profiling.html +1018 -0
  34. data/doc/guides/html/caching_with_rails.html +583 -0
  35. data/doc/guides/html/command_line.html +434 -0
  36. data/doc/guides/html/configuring.html +438 -0
  37. data/doc/guides/html/creating_plugins.html +1594 -0
  38. data/doc/guides/html/debugging_rails_applications.html +1175 -0
  39. data/doc/guides/html/finders.html +1090 -0
  40. data/doc/guides/html/form_helpers.html +638 -0
  41. data/doc/guides/html/getting_started_with_rails.html +2066 -0
  42. data/doc/guides/html/index.html +349 -0
  43. data/doc/guides/html/layouts_and_rendering.html +1406 -0
  44. data/doc/guides/html/migrations.html +921 -0
  45. data/doc/guides/html/routing_outside_in.html +2213 -0
  46. data/doc/guides/html/security.html +1346 -0
  47. data/doc/guides/html/testing_rails_applications.html +1859 -0
  48. data/doc/guides/source/2_2_release_notes.txt +435 -0
  49. data/doc/guides/source/actioncontroller_basics/changelog.txt +5 -0
  50. data/doc/guides/source/actioncontroller_basics/cookies.txt +34 -0
  51. data/doc/guides/source/actioncontroller_basics/csrf.txt +32 -0
  52. data/doc/guides/source/actioncontroller_basics/filters.txt +119 -0
  53. data/doc/guides/source/actioncontroller_basics/http_auth.txt +24 -0
  54. data/doc/guides/source/actioncontroller_basics/index.txt +40 -0
  55. data/doc/guides/source/actioncontroller_basics/introduction.txt +9 -0
  56. data/doc/guides/source/actioncontroller_basics/methods.txt +39 -0
  57. data/doc/guides/source/actioncontroller_basics/parameter_filtering.txt +14 -0
  58. data/doc/guides/source/actioncontroller_basics/params.txt +93 -0
  59. data/doc/guides/source/actioncontroller_basics/request_response_objects.txt +43 -0
  60. data/doc/guides/source/actioncontroller_basics/rescue.txt +67 -0
  61. data/doc/guides/source/actioncontroller_basics/session.txt +187 -0
  62. data/doc/guides/source/actioncontroller_basics/streaming.txt +91 -0
  63. data/doc/guides/source/actioncontroller_basics/verification.txt +40 -0
  64. data/doc/guides/source/active_record_basics.txt +181 -0
  65. data/doc/guides/source/activerecord_validations_callbacks.txt +404 -0
  66. data/doc/guides/source/association_basics.txt +1840 -0
  67. data/doc/guides/source/authors.txt +39 -0
  68. data/doc/guides/source/benchmarking_and_profiling/appendix.txt +95 -0
  69. data/doc/guides/source/benchmarking_and_profiling/digging_deeper.txt +105 -0
  70. data/doc/guides/source/benchmarking_and_profiling/edge_rails_features.txt +185 -0
  71. data/doc/guides/source/benchmarking_and_profiling/gameplan.txt +27 -0
  72. data/doc/guides/source/benchmarking_and_profiling/index.txt +242 -0
  73. data/doc/guides/source/benchmarking_and_profiling/rubyprof.txt +179 -0
  74. data/doc/guides/source/benchmarking_and_profiling/statistics.txt +57 -0
  75. data/doc/guides/source/caching_with_rails.txt +367 -0
  76. data/doc/guides/source/command_line.txt +147 -0
  77. data/doc/guides/source/configuring.txt +225 -0
  78. data/doc/guides/source/creating_plugins/acts_as_yaffle.txt +191 -0
  79. data/doc/guides/source/creating_plugins/appendix.txt +46 -0
  80. data/doc/guides/source/creating_plugins/controllers.txt +59 -0
  81. data/doc/guides/source/creating_plugins/core_ext.txt +123 -0
  82. data/doc/guides/source/creating_plugins/custom_route.txt +69 -0
  83. data/doc/guides/source/creating_plugins/gem.txt +1 -0
  84. data/doc/guides/source/creating_plugins/generator_method.txt +89 -0
  85. data/doc/guides/source/creating_plugins/helpers.txt +51 -0
  86. data/doc/guides/source/creating_plugins/index.txt +52 -0
  87. data/doc/guides/source/creating_plugins/migration_generator.txt +156 -0
  88. data/doc/guides/source/creating_plugins/models.txt +76 -0
  89. data/doc/guides/source/creating_plugins/odds_and_ends.txt +69 -0
  90. data/doc/guides/source/creating_plugins/test_setup.txt +230 -0
  91. data/doc/guides/source/debugging_rails_applications.txt +733 -0
  92. data/doc/guides/source/finders.txt +668 -0
  93. data/doc/guides/source/form_helpers.txt +345 -0
  94. data/doc/guides/source/getting_started_with_rails.txt +1256 -0
  95. data/doc/guides/source/images/belongs_to.png +0 -0
  96. data/doc/guides/source/images/bullet.gif +0 -0
  97. data/doc/guides/source/images/csrf.png +0 -0
  98. data/doc/guides/source/images/habtm.png +0 -0
  99. data/doc/guides/source/images/has_many.png +0 -0
  100. data/doc/guides/source/images/has_many_through.png +0 -0
  101. data/doc/guides/source/images/has_one.png +0 -0
  102. data/doc/guides/source/images/has_one_through.png +0 -0
  103. data/doc/guides/source/images/header_backdrop.png +0 -0
  104. data/doc/guides/source/images/icons/README +5 -0
  105. data/doc/guides/source/images/icons/callouts/1.png +0 -0
  106. data/doc/guides/source/images/icons/callouts/10.png +0 -0
  107. data/doc/guides/source/images/icons/callouts/11.png +0 -0
  108. data/doc/guides/source/images/icons/callouts/12.png +0 -0
  109. data/doc/guides/source/images/icons/callouts/13.png +0 -0
  110. data/doc/guides/source/images/icons/callouts/14.png +0 -0
  111. data/doc/guides/source/images/icons/callouts/15.png +0 -0
  112. data/doc/guides/source/images/icons/callouts/2.png +0 -0
  113. data/doc/guides/source/images/icons/callouts/3.png +0 -0
  114. data/doc/guides/source/images/icons/callouts/4.png +0 -0
  115. data/doc/guides/source/images/icons/callouts/5.png +0 -0
  116. data/doc/guides/source/images/icons/callouts/6.png +0 -0
  117. data/doc/guides/source/images/icons/callouts/7.png +0 -0
  118. data/doc/guides/source/images/icons/callouts/8.png +0 -0
  119. data/doc/guides/source/images/icons/callouts/9.png +0 -0
  120. data/doc/guides/source/images/icons/caution.png +0 -0
  121. data/doc/guides/source/images/icons/example.png +0 -0
  122. data/doc/guides/source/images/icons/home.png +0 -0
  123. data/doc/guides/source/images/icons/important.png +0 -0
  124. data/doc/guides/source/images/icons/next.png +0 -0
  125. data/doc/guides/source/images/icons/note.png +0 -0
  126. data/doc/guides/source/images/icons/prev.png +0 -0
  127. data/doc/guides/source/images/icons/tip.png +0 -0
  128. data/doc/guides/source/images/icons/up.png +0 -0
  129. data/doc/guides/source/images/icons/warning.png +0 -0
  130. data/doc/guides/source/images/polymorphic.png +0 -0
  131. data/doc/guides/source/images/rails_logo_remix.gif +0 -0
  132. data/doc/guides/source/images/ruby_on_rails_by_mike_rundle2.gif +0 -0
  133. data/doc/guides/source/images/session_fixation.png +0 -0
  134. data/doc/guides/source/index.txt +118 -0
  135. data/doc/guides/source/layouts_and_rendering.txt +982 -0
  136. data/doc/guides/source/migrations/anatomy_of_a_migration.txt +85 -0
  137. data/doc/guides/source/migrations/changelog.txt +5 -0
  138. data/doc/guides/source/migrations/creating_a_migration.txt +109 -0
  139. data/doc/guides/source/migrations/foreign_keys.txt +8 -0
  140. data/doc/guides/source/migrations/index.txt +22 -0
  141. data/doc/guides/source/migrations/rakeing_around.txt +111 -0
  142. data/doc/guides/source/migrations/scheming.txt +47 -0
  143. data/doc/guides/source/migrations/using_models_in_migrations.txt +46 -0
  144. data/doc/guides/source/migrations/writing_a_migration.txt +159 -0
  145. data/doc/guides/source/routing_outside_in.txt +986 -0
  146. data/doc/guides/source/security.txt +984 -0
  147. data/doc/guides/source/stylesheets/base.css +358 -0
  148. data/doc/guides/source/stylesheets/forms.css +35 -0
  149. data/doc/guides/source/stylesheets/more.css +82 -0
  150. data/doc/guides/source/templates/guides.html.erb +97 -0
  151. data/doc/guides/source/templates/inline.css +165 -0
  152. data/doc/guides/source/testing_rails_applications.txt +995 -0
  153. data/environments/boot.rb +2 -2
  154. data/environments/environment.rb +9 -1
  155. data/environments/production.rb +3 -1
  156. data/helpers/performance_test.rb +9 -0
  157. data/html/500.html +4 -1
  158. data/html/javascripts/controls.js +72 -72
  159. data/html/javascripts/dragdrop.js +165 -164
  160. data/html/javascripts/effects.js +173 -165
  161. data/html/javascripts/prototype.js +362 -267
  162. data/lib/commands/console.rb +13 -0
  163. data/lib/commands/dbconsole.rb +2 -2
  164. data/lib/commands/ncgi/listener +2 -2
  165. data/lib/commands/ncgi/tracker +2 -2
  166. data/lib/commands/plugin.rb +41 -24
  167. data/lib/commands/process/spawner.rb +4 -4
  168. data/lib/commands/runner.rb +1 -1
  169. data/lib/commands/server.rb +11 -1
  170. data/lib/commands/servers/thin.rb +25 -0
  171. data/lib/fcgi_handler.rb +1 -3
  172. data/lib/initializer.rb +162 -39
  173. data/lib/performance_test_help.rb +5 -0
  174. data/lib/rails/gem_builder.rb +3 -3
  175. data/lib/rails/gem_dependency.rb +155 -33
  176. data/lib/rails/mongrel_server/commands.rb +1 -1
  177. data/lib/rails/plugin.rb +10 -2
  178. data/lib/rails/rack.rb +6 -0
  179. data/lib/rails/rack/logger.rb +28 -0
  180. data/lib/rails/rack/static.rb +35 -0
  181. data/lib/rails/vendor_gem_source_index.rb +140 -0
  182. data/lib/rails/version.rb +1 -1
  183. data/lib/rails_generator/commands.rb +10 -16
  184. data/lib/rails_generator/generated_attribute.rb +4 -0
  185. data/lib/rails_generator/generators/applications/app/app_generator.rb +9 -3
  186. data/lib/rails_generator/generators/components/controller/controller_generator.rb +1 -1
  187. data/lib/rails_generator/generators/components/controller/templates/functional_test.rb +1 -1
  188. data/lib/rails_generator/generators/components/integration_test/integration_test_generator.rb +1 -1
  189. data/lib/rails_generator/generators/components/integration_test/templates/integration_test.rb +2 -2
  190. data/lib/rails_generator/generators/components/mailer/mailer_generator.rb +1 -1
  191. data/lib/rails_generator/generators/components/mailer/templates/unit_test.rb +2 -3
  192. data/lib/rails_generator/generators/components/model/model_generator.rb +1 -1
  193. data/lib/rails_generator/generators/components/model/templates/model.rb +3 -0
  194. data/lib/rails_generator/generators/components/model/templates/unit_test.rb +1 -1
  195. data/lib/rails_generator/generators/components/observer/observer_generator.rb +1 -1
  196. data/lib/rails_generator/generators/components/observer/templates/unit_test.rb +1 -1
  197. data/lib/rails_generator/generators/components/performance_test/USAGE +8 -0
  198. data/lib/rails_generator/generators/components/performance_test/performance_test_generator.rb +16 -0
  199. data/lib/rails_generator/generators/components/performance_test/templates/performance_test.rb +9 -0
  200. data/lib/rails_generator/generators/components/plugin/plugin_generator.rb +11 -11
  201. data/lib/rails_generator/generators/components/plugin/templates/Rakefile +1 -0
  202. data/lib/rails_generator/generators/components/plugin/templates/test_helper.rb +3 -0
  203. data/lib/rails_generator/generators/components/plugin/templates/unit_test.rb +4 -4
  204. data/lib/rails_generator/generators/components/resource/resource_generator.rb +2 -2
  205. data/lib/rails_generator/generators/components/resource/templates/functional_test.rb +1 -1
  206. data/lib/rails_generator/generators/components/scaffold/USAGE +9 -5
  207. data/lib/rails_generator/generators/components/scaffold/scaffold_generator.rb +10 -3
  208. data/lib/rails_generator/generators/components/scaffold/templates/functional_test.rb +7 -7
  209. data/lib/rails_generator/scripts.rb +1 -1
  210. data/lib/rails_generator/scripts/destroy.rb +6 -7
  211. data/lib/rails_generator/secret_key_generator.rb +5 -147
  212. data/lib/tasks/annotations.rake +8 -11
  213. data/lib/tasks/databases.rake +42 -15
  214. data/lib/tasks/documentation.rake +15 -0
  215. data/lib/tasks/framework.rake +5 -1
  216. data/lib/tasks/gems.rake +24 -8
  217. data/lib/tasks/misc.rake +3 -4
  218. data/lib/tasks/testing.rake +25 -4
  219. data/lib/test_help.rb +1 -2
  220. data/lib/webrick_server.rb +2 -11
  221. metadata +165 -9
@@ -0,0 +1,2585 @@
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>A Guide to Active Record Associations</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="#_why_associations">Why Associations?</a>
203
+ </li>
204
+ <li>
205
+ <a href="#_the_types_of_associations">The Types of Associations</a>
206
+ <ul>
207
+
208
+ <li><a href="#_the_tt_belongs_to_tt_association">The <tt>belongs_to</tt> Association</a></li>
209
+
210
+ <li><a href="#_the_tt_has_one_tt_association">The <tt>has_one</tt> Association</a></li>
211
+
212
+ <li><a href="#_the_tt_has_many_tt_association">The <tt>has_many</tt> Association</a></li>
213
+
214
+ <li><a href="#_the_tt_has_many_through_tt_association">The <tt>has_many :through</tt> Association</a></li>
215
+
216
+ <li><a href="#_the_tt_has_one_through_tt_association">The <tt>has_one :through</tt> Association</a></li>
217
+
218
+ <li><a href="#_the_tt_has_and_belongs_to_many_tt_association">The <tt>has_and_belongs_to_many</tt> Association</a></li>
219
+
220
+ <li><a href="#_choosing_between_tt_belongs_to_tt_and_tt_has_one_tt">Choosing Between <tt>belongs_to</tt> and <tt>has_one</tt></a></li>
221
+
222
+ <li><a href="#_choosing_between_tt_has_many_through_tt_and_tt_has_and_belongs_to_many_tt">Choosing Between <tt>has_many :through</tt> and <tt>has_and_belongs_to_many</tt></a></li>
223
+
224
+ <li><a href="#_polymorphic_associations">Polymorphic Associations</a></li>
225
+
226
+ <li><a href="#_self_joins">Self Joins</a></li>
227
+
228
+ </ul>
229
+ </li>
230
+ <li>
231
+ <a href="#_tips_tricks_and_warnings">Tips, Tricks, and Warnings</a>
232
+ <ul>
233
+
234
+ <li><a href="#_controlling_caching">Controlling Caching</a></li>
235
+
236
+ <li><a href="#_avoiding_name_collisions">Avoiding Name Collisions</a></li>
237
+
238
+ <li><a href="#_updating_the_schema">Updating the Schema</a></li>
239
+
240
+ <li><a href="#_controlling_association_scope">Controlling Association Scope</a></li>
241
+
242
+ </ul>
243
+ </li>
244
+ <li>
245
+ <a href="#_detailed_association_reference">Detailed Association Reference</a>
246
+ <ul>
247
+
248
+ <li><a href="#_the_tt_belongs_to_tt_association_2">The <tt>belongs_to</tt> Association</a></li>
249
+
250
+ <li><a href="#_the_has_one_association">The has_one Association</a></li>
251
+
252
+ <li><a href="#_the_has_many_association">The has_many Association</a></li>
253
+
254
+ <li><a href="#_the_tt_has_and_belongs_to_many_tt_association_2">The <tt>has_and_belongs_to_many</tt> Association</a></li>
255
+
256
+ <li><a href="#_association_callbacks">Association Callbacks</a></li>
257
+
258
+ <li><a href="#_association_extensions">Association Extensions</a></li>
259
+
260
+ </ul>
261
+ </li>
262
+ <li>
263
+ <a href="#_changelog">Changelog</a>
264
+ </li>
265
+ </ol>
266
+ </div>
267
+
268
+ <div id="content">
269
+ <h1>A Guide to Active Record Associations</h1>
270
+ <div id="preamble">
271
+ <div class="sectionbody">
272
+ <div class="para"><p>This guide covers the association features of Active Record. By referring to this guide, you will be able to:</p></div>
273
+ <div class="ilist"><ul>
274
+ <li>
275
+ <p>
276
+ Declare associations between Active Record models
277
+ </p>
278
+ </li>
279
+ <li>
280
+ <p>
281
+ Understand the various types of Active Record associations
282
+ </p>
283
+ </li>
284
+ <li>
285
+ <p>
286
+ Use the methods added to your models by creating associations
287
+ </p>
288
+ </li>
289
+ </ul></div>
290
+ </div>
291
+ </div>
292
+ <h2 id="_why_associations">1. Why Associations?</h2>
293
+ <div class="sectionbody">
294
+ <div class="para"><p>Why do we need associations between models? Because they make common operations simpler and easier in your code. For example, consider a simple Rails application that includes a model for customers and a model for orders. Each customer can have many orders. Without associations, the model declarations would look like this:</p></div>
295
+ <div class="listingblock">
296
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
297
+ by Lorenzo Bettini
298
+ http://www.lorenzobettini.it
299
+ http://www.gnu.org/software/src-highlite -->
300
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
301
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
302
+
303
+ <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Order <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
304
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
305
+ </tt></pre></div></div>
306
+ <div class="para"><p>Now, suppose we wanted to add a new order for an existing customer. We'd need to do something like this:</p></div>
307
+ <div class="listingblock">
308
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
309
+ by Lorenzo Bettini
310
+ http://www.lorenzobettini.it
311
+ http://www.gnu.org/software/src-highlite -->
312
+ <pre><tt><span style="color: #009900">@order</span> <span style="color: #990000">=</span> Order<span style="color: #990000">.</span>create<span style="color: #990000">(:</span>order_date <span style="color: #990000">=&gt;</span> Time<span style="color: #990000">.</span>now<span style="color: #990000">,</span> <span style="color: #990000">:</span>customer_id <span style="color: #990000">=&gt;</span> <span style="color: #009900">@customer</span><span style="color: #990000">.</span>id<span style="color: #990000">)</span>
313
+ </tt></pre></div></div>
314
+ <div class="para"><p>Or consider deleting a customer, and ensuring that all of its orders get deleted as well:</p></div>
315
+ <div class="listingblock">
316
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
317
+ by Lorenzo Bettini
318
+ http://www.lorenzobettini.it
319
+ http://www.gnu.org/software/src-highlite -->
320
+ <pre><tt><span style="color: #009900">@orders</span> <span style="color: #990000">=</span> Order<span style="color: #990000">.</span>find_by_customer_id<span style="color: #990000">(</span><span style="color: #009900">@customer</span><span style="color: #990000">.</span>id<span style="color: #990000">)</span>
321
+ <span style="color: #009900">@orders</span><span style="color: #990000">.</span>each <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>order<span style="color: #990000">|</span>
322
+ order<span style="color: #990000">.</span>destroy
323
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
324
+ <span style="color: #009900">@customer</span><span style="color: #990000">.</span>destroy
325
+ </tt></pre></div></div>
326
+ <div class="para"><p>With Active Record associations, we can streamline these - and other - operations by declaratively telling Rails that there is a connection between the two models. Here's the revised code for setting up customers and orders:</p></div>
327
+ <div class="listingblock">
328
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
329
+ by Lorenzo Bettini
330
+ http://www.lorenzobettini.it
331
+ http://www.gnu.org/software/src-highlite -->
332
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
333
+ has_many <span style="color: #990000">:</span>orders
334
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
335
+
336
+ <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Order <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
337
+ belongs_to <span style="color: #990000">:</span>customer
338
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
339
+ </tt></pre></div></div>
340
+ <div class="para"><p>With this change, creating a new order for a particular customer is easier:</p></div>
341
+ <div class="listingblock">
342
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
343
+ by Lorenzo Bettini
344
+ http://www.lorenzobettini.it
345
+ http://www.gnu.org/software/src-highlite -->
346
+ <pre><tt><span style="color: #009900">@order</span> <span style="color: #990000">=</span> <span style="color: #009900">@customer</span><span style="color: #990000">.</span>orders<span style="color: #990000">.</span>create<span style="color: #990000">(:</span>order_date <span style="color: #990000">=&gt;</span> Time<span style="color: #990000">.</span>now<span style="color: #990000">)</span>
347
+ </tt></pre></div></div>
348
+ <div class="para"><p>Deleting a customer and all of its orders is <em>much</em> easier:</p></div>
349
+ <div class="listingblock">
350
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
351
+ by Lorenzo Bettini
352
+ http://www.lorenzobettini.it
353
+ http://www.gnu.org/software/src-highlite -->
354
+ <pre><tt><span style="color: #009900">@customer</span><span style="color: #990000">.</span>destroy
355
+ </tt></pre></div></div>
356
+ <div class="para"><p>To learn more about the different types of associations, read the next section of this Guide. That's followed by some tips and tricks for working with associations, and then by a complete reference to the methods and options for associations in Rails.</p></div>
357
+ </div>
358
+ <h2 id="_the_types_of_associations">2. The Types of Associations</h2>
359
+ <div class="sectionbody">
360
+ <div class="para"><p>In Rails, an <em>association</em> is a connection between two Active Record models. Associations are implemented using macro-style calls, so that you can declaratively add features to your models. For example, by declaring that one model <tt>belongs_to</tt> another, you instruct Rails to maintain Primary Key-Foreign Key information between instances of the two models, and you also get a number of utility methods added to your model. Rails supports six types of association:</p></div>
361
+ <div class="ilist"><ul>
362
+ <li>
363
+ <p>
364
+ <tt>belongs_to</tt>
365
+ </p>
366
+ </li>
367
+ <li>
368
+ <p>
369
+ <tt>has_one</tt>
370
+ </p>
371
+ </li>
372
+ <li>
373
+ <p>
374
+ <tt>has_many</tt>
375
+ </p>
376
+ </li>
377
+ <li>
378
+ <p>
379
+ <tt>has_many :through</tt>
380
+ </p>
381
+ </li>
382
+ <li>
383
+ <p>
384
+ <tt>has_one :through</tt>
385
+ </p>
386
+ </li>
387
+ <li>
388
+ <p>
389
+ <tt>has_and_belongs_to_many</tt>
390
+ </p>
391
+ </li>
392
+ </ul></div>
393
+ <div class="para"><p>In the remainder of this guide, you'll learn how to declare and use the various forms of associations. But first, a quick introduction to the situations where each association type is appropriate.</p></div>
394
+ <h3 id="_the_tt_belongs_to_tt_association">2.1. The <tt>belongs_to</tt> Association</h3>
395
+ <div class="para"><p>A <tt>belongs_to</tt> association sets up a one-to-one connection with another model, such that each instance of the declaring model "belongs to" one instance of the other model. For example, if your application includes customers and orders, and each order can be assigned to exactly one customer, you'd declare the order model this way:</p></div>
396
+ <div class="listingblock">
397
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
398
+ by Lorenzo Bettini
399
+ http://www.lorenzobettini.it
400
+ http://www.gnu.org/software/src-highlite -->
401
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Order <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
402
+ belongs_to <span style="color: #990000">:</span>customer
403
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
404
+ </tt></pre></div></div>
405
+ <div class="para"><p><span class="image">
406
+ <img src="images/belongs_to.png" alt="belongs_to Association Diagram" title="belongs_to Association Diagram" />
407
+ </span></p></div>
408
+ <h3 id="_the_tt_has_one_tt_association">2.2. The <tt>has_one</tt> Association</h3>
409
+ <div class="para"><p>A <tt>has_one</tt> association also sets up a one-to-one connection with another model, but with somewhat different semantics (and consequences). This association indicates that each instance of a model contains or possesses one instance of another model. For example, if each supplier in your application has only one account, you'd declare the supplier model like this:</p></div>
410
+ <div class="listingblock">
411
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
412
+ by Lorenzo Bettini
413
+ http://www.lorenzobettini.it
414
+ http://www.gnu.org/software/src-highlite -->
415
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Supplier <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
416
+ has_one <span style="color: #990000">:</span>account
417
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
418
+ </tt></pre></div></div>
419
+ <div class="para"><p><span class="image">
420
+ <img src="images/has_one.png" alt="has_one Association Diagram" title="has_one Association Diagram" />
421
+ </span></p></div>
422
+ <h3 id="_the_tt_has_many_tt_association">2.3. The <tt>has_many</tt> Association</h3>
423
+ <div class="para"><p>A <tt>has_many</tt> association indicates a one-to-many connection with another model. You'll often find this association on the "other side" of a <tt>belongs_to</tt> association. This association indicates that each instance of the model has zero or more instances of another model. For example, in an application containing customers and orders, the customer model could be declared like this:</p></div>
424
+ <div class="listingblock">
425
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
426
+ by Lorenzo Bettini
427
+ http://www.lorenzobettini.it
428
+ http://www.gnu.org/software/src-highlite -->
429
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
430
+ has_many <span style="color: #990000">:</span>orders
431
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
432
+ </tt></pre></div></div>
433
+ <div class="admonitionblock">
434
+ <table><tr>
435
+ <td class="icon">
436
+ <img src="./images/icons/note.png" alt="Note" />
437
+ </td>
438
+ <td class="content">The name of the other model is pluralized when declaring a <tt>has_many</tt> association.</td>
439
+ </tr></table>
440
+ </div>
441
+ <div class="para"><p><span class="image">
442
+ <img src="images/has_many.png" alt="has_many Association Diagram" title="has_many Association Diagram" />
443
+ </span></p></div>
444
+ <h3 id="_the_tt_has_many_through_tt_association">2.4. The <tt>has_many :through</tt> Association</h3>
445
+ <div class="para"><p>A <tt>has_many :through</tt> association is often used to set up a many-to-many connection with another model. This association indicates that the declaring model can be matched with zero or more instances of another model by proceeding <em>through</em> a third model. For example, consider a medical practice where patients make appointments to see physicians. The relevant association declarations could look like this:</p></div>
446
+ <div class="listingblock">
447
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
448
+ by Lorenzo Bettini
449
+ http://www.lorenzobettini.it
450
+ http://www.gnu.org/software/src-highlite -->
451
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Physician <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
452
+ has_many <span style="color: #990000">:</span>appointments
453
+ has_many <span style="color: #990000">:</span>patients<span style="color: #990000">,</span> <span style="color: #990000">:</span>through <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>appointments
454
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
455
+
456
+ <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Appointment <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
457
+ belongs_to <span style="color: #990000">:</span>physician
458
+ belongs_to <span style="color: #990000">:</span>patient
459
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
460
+
461
+ <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Patient <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
462
+ has_many <span style="color: #990000">:</span>appointments
463
+ has_many <span style="color: #990000">:</span>physicians<span style="color: #990000">,</span> <span style="color: #990000">:</span>through <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>appointments
464
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
465
+ </tt></pre></div></div>
466
+ <div class="para"><p><span class="image">
467
+ <img src="images/has_many_through.png" alt="has_many :through Association Diagram" title="has_many :through Association Diagram" />
468
+ </span></p></div>
469
+ <div class="para"><p>The <tt>has_many :through</tt> association is also useful for setting up "shortcuts" through nested :<tt>has_many</tt> associations. For example, if a document has many sections, and a section has many paragraphs, you may sometimes want to get a simple collection of all paragraphs in the document. You could set that up this way:</p></div>
470
+ <div class="listingblock">
471
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
472
+ by Lorenzo Bettini
473
+ http://www.lorenzobettini.it
474
+ http://www.gnu.org/software/src-highlite -->
475
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Document <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
476
+ has_many <span style="color: #990000">:</span>sections
477
+ has_many <span style="color: #990000">:</span>paragraphs<span style="color: #990000">,</span> <span style="color: #990000">:</span>through <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>sections
478
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
479
+
480
+ <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Section <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
481
+ belongs_to <span style="color: #990000">:</span>document
482
+ has_many <span style="color: #990000">:</span>paragraphs
483
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
484
+
485
+ <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Paragraph <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
486
+ belongs_to <span style="color: #990000">:</span>section
487
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
488
+ </tt></pre></div></div>
489
+ <h3 id="_the_tt_has_one_through_tt_association">2.5. The <tt>has_one :through</tt> Association</h3>
490
+ <div class="para"><p>A <tt>has_one :through</tt> association sets up a one-to-one connection with another model. This association indicates that the declaring model can be matched with one instance of another model by proceeding <em>through</em> a third model. For example, if each supplier has one account, and each account is associated with one account history, then the customer model could look like this:</p></div>
491
+ <div class="listingblock">
492
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
493
+ by Lorenzo Bettini
494
+ http://www.lorenzobettini.it
495
+ http://www.gnu.org/software/src-highlite -->
496
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Supplier <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
497
+ has_one <span style="color: #990000">:</span>account
498
+ has_one <span style="color: #990000">:</span>account_history<span style="color: #990000">,</span> <span style="color: #990000">:</span>through <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>account
499
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
500
+
501
+ <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Account <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
502
+ belongs_to <span style="color: #990000">:</span>supplier
503
+ has_one <span style="color: #990000">:</span>account_history
504
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
505
+
506
+ <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> AccountHistory <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
507
+ belongs_to <span style="color: #990000">:</span>account
508
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
509
+ </tt></pre></div></div>
510
+ <div class="para"><p><span class="image">
511
+ <img src="images/has_one_through.png" alt="has_one :through Association Diagram" title="has_one :through Association Diagram" />
512
+ </span></p></div>
513
+ <h3 id="_the_tt_has_and_belongs_to_many_tt_association">2.6. The <tt>has_and_belongs_to_many</tt> Association</h3>
514
+ <div class="para"><p>A <tt>has_and_belongs_to_many</tt> association creates a direct many-to-many connection with another model, with no intervening model. For example, if your application includes assemblies and parts, with each assembly having many parts and each part appearing in many assemblies, you could declare the models this way:</p></div>
515
+ <div class="listingblock">
516
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
517
+ by Lorenzo Bettini
518
+ http://www.lorenzobettini.it
519
+ http://www.gnu.org/software/src-highlite -->
520
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Assembly <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
521
+ has_and_belongs_to_many <span style="color: #990000">:</span>parts
522
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
523
+
524
+ <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Part <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
525
+ has_and_belongs_to_many <span style="color: #990000">:</span>assemblies
526
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
527
+ </tt></pre></div></div>
528
+ <div class="para"><p><span class="image">
529
+ <img src="images/habtm.png" alt="has_and_belongs_to_many Association Diagram" title="has_and_belongs_to_many Association Diagram" />
530
+ </span></p></div>
531
+ <h3 id="_choosing_between_tt_belongs_to_tt_and_tt_has_one_tt">2.7. Choosing Between <tt>belongs_to</tt> and <tt>has_one</tt></h3>
532
+ <div class="para"><p>If you want to set up a 1-1 relationship between two models, you'll need to add <tt>belongs_to</tt> to one, and <tt>has_one</tt> to the other. How do you know which is which?</p></div>
533
+ <div class="para"><p>The distinction is in where you place the foreign key (it goes on the table for the class declaring the <tt>belongs_to</tt> association), but you should give some thought to the actual meaning of the data as well. The <tt>has_one</tt> relationship says that one of something is yours - that is, that something points back to you. For example, it makes more sense to say that a supplier owns an account than that an account owns a supplier. This suggests that the correct relationships are like this:</p></div>
534
+ <div class="listingblock">
535
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
536
+ by Lorenzo Bettini
537
+ http://www.lorenzobettini.it
538
+ http://www.gnu.org/software/src-highlite -->
539
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Supplier <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
540
+ has_one <span style="color: #990000">:</span>account
541
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
542
+
543
+ <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Account <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
544
+ belongs_to <span style="color: #990000">:</span>supplier
545
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
546
+ </tt></pre></div></div>
547
+ <div class="para"><p>The corresponding migration might look like this:</p></div>
548
+ <div class="listingblock">
549
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
550
+ by Lorenzo Bettini
551
+ http://www.lorenzobettini.it
552
+ http://www.gnu.org/software/src-highlite -->
553
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> CreateSuppliers <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Migration
554
+ <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>up
555
+ create_table <span style="color: #990000">:</span>suppliers <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>t<span style="color: #990000">|</span>
556
+ t<span style="color: #990000">.</span>string <span style="color: #990000">:</span>name
557
+ t<span style="color: #990000">.</span>timestamps
558
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
559
+
560
+ create_table <span style="color: #990000">:</span>accounts <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>t<span style="color: #990000">|</span>
561
+ t<span style="color: #990000">.</span>integer <span style="color: #990000">:</span>supplier_id
562
+ t<span style="color: #990000">.</span>string <span style="color: #990000">:</span>account_number
563
+ t<span style="color: #990000">.</span>timestamps
564
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
565
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
566
+
567
+ <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>down
568
+ drop_table <span style="color: #990000">:</span>accounts
569
+ drop_table <span style="color: #990000">:</span>suppliers
570
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
571
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
572
+ </tt></pre></div></div>
573
+ <div class="admonitionblock">
574
+ <table><tr>
575
+ <td class="icon">
576
+ <img src="./images/icons/note.png" alt="Note" />
577
+ </td>
578
+ <td class="content">Using <tt>t.integer :supplier_id</tt> makes the foreign key naming obvious and implicit. In current versions of Rails, you can abstract away this implementation detail by using <tt>t.references :supplier</tt> instead.</td>
579
+ </tr></table>
580
+ </div>
581
+ <h3 id="_choosing_between_tt_has_many_through_tt_and_tt_has_and_belongs_to_many_tt">2.8. Choosing Between <tt>has_many :through</tt> and <tt>has_and_belongs_to_many</tt></h3>
582
+ <div class="para"><p>Rails offers two different ways to declare a many-to-many relationship between models. The simpler way is to use <tt>has_and_belongs_to_many</tt>, which allows you to make the association directly:</p></div>
583
+ <div class="listingblock">
584
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
585
+ by Lorenzo Bettini
586
+ http://www.lorenzobettini.it
587
+ http://www.gnu.org/software/src-highlite -->
588
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Assembly <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
589
+ has_and_belongs_to_many <span style="color: #990000">:</span>parts
590
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
591
+
592
+ <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Part <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
593
+ has_and_belongs_to_many <span style="color: #990000">:</span>assemblies
594
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
595
+ </tt></pre></div></div>
596
+ <div class="para"><p>The second way to declare a many-to-many relationship is to use <tt>has_many :through</tt>. This makes the association indirectly, through a join model:</p></div>
597
+ <div class="listingblock">
598
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
599
+ by Lorenzo Bettini
600
+ http://www.lorenzobettini.it
601
+ http://www.gnu.org/software/src-highlite -->
602
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Assembly <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
603
+ has_many <span style="color: #990000">:</span>manifests
604
+ has_many <span style="color: #990000">:</span>parts<span style="color: #990000">,</span> <span style="color: #990000">:</span>through <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>manifests
605
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
606
+
607
+ <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Manifest <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
608
+ belongs_to <span style="color: #990000">:</span>assembly
609
+ belongs_to <span style="color: #990000">:</span>part
610
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
611
+
612
+ <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Part <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
613
+ has_many <span style="color: #990000">:</span>manifests
614
+ has_many <span style="color: #990000">:</span>assemblies<span style="color: #990000">,</span> <span style="color: #990000">:</span>through <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>manifests
615
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
616
+ </tt></pre></div></div>
617
+ <div class="para"><p>The simplest rule of thumb is that you should set up a <tt>has_many :through</tt> relationship if you need to work with the relationship model as an independent entity. If you don't need to do anything with the relationship model, it may be simpler to set up a <tt>has_and_belongs_to_many</tt> relationship (though you'll need to remember to create the joining table).</p></div>
618
+ <div class="para"><p>You should use <tt>has_many :through</tt> if you need validations, callbacks, or extra attributes on the join model.</p></div>
619
+ <h3 id="_polymorphic_associations">2.9. Polymorphic Associations</h3>
620
+ <div class="para"><p>A slightly more advanced twist on associations is the <em>polymorphic association</em>. With polymorphic associations, a model can belong to more than one other model, on a single association. For example, you might have a picture model that belongs to either an employee model or a product model. Here's how this could be declared:</p></div>
621
+ <div class="listingblock">
622
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
623
+ by Lorenzo Bettini
624
+ http://www.lorenzobettini.it
625
+ http://www.gnu.org/software/src-highlite -->
626
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Picture <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
627
+ belongs_to <span style="color: #990000">:</span>imageable<span style="color: #990000">,</span> <span style="color: #990000">:</span>polymorphic <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
628
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
629
+
630
+ <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Employee <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
631
+ has_many <span style="color: #990000">:</span>pictures<span style="color: #990000">,</span> <span style="color: #990000">:</span>as <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>imageable
632
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
633
+
634
+ <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Product <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
635
+ has_many <span style="color: #990000">:</span>pictures<span style="color: #990000">,</span> <span style="color: #990000">:</span>as <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>imageable
636
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
637
+ </tt></pre></div></div>
638
+ <div class="para"><p>You can think of a polymorphic <tt>belongs_to</tt> declaration as setting up an interface that any other model can use. From an instance of the <tt>Employee</tt> model, you can retrieve a collection of pictures: <tt>@employee.pictures</tt>. Similarly, you can retrieve <tt>@product.pictures</tt>. If you have an instance of the <tt>Picture</tt> model, you can get to its parent via <tt>@picture.imageable</tt>. To make this work, you need to declare both a foreign key column and a type column in the model that declares the polymorphic interface:</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">class</span></span> CreatePictures <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Migration
645
+ <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>up
646
+ create_table <span style="color: #990000">:</span>pictures <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>t<span style="color: #990000">|</span>
647
+ t<span style="color: #990000">.</span>string <span style="color: #990000">:</span>name
648
+ t<span style="color: #990000">.</span>integer <span style="color: #990000">:</span>imageable_id
649
+ t<span style="color: #990000">.</span>string <span style="color: #990000">:</span>imageable_type
650
+ t<span style="color: #990000">.</span>timestamps
651
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
652
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
653
+
654
+ <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>down
655
+ drop_table <span style="color: #990000">:</span>pictures
656
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
657
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
658
+ </tt></pre></div></div>
659
+ <div class="para"><p>This migration can be simplified by using the <tt>t.references</tt> form:</p></div>
660
+ <div class="listingblock">
661
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
662
+ by Lorenzo Bettini
663
+ http://www.lorenzobettini.it
664
+ http://www.gnu.org/software/src-highlite -->
665
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> CreatePictures <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Migration
666
+ <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>up
667
+ create_table <span style="color: #990000">:</span>pictures <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>t<span style="color: #990000">|</span>
668
+ t<span style="color: #990000">.</span>string <span style="color: #990000">:</span>name
669
+ t<span style="color: #990000">.</span>references <span style="color: #990000">:</span>imageable<span style="color: #990000">,</span> <span style="color: #990000">:</span>polymorphic <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
670
+ t<span style="color: #990000">.</span>timestamps
671
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
672
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
673
+
674
+ <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>down
675
+ drop_table <span style="color: #990000">:</span>pictures
676
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
677
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
678
+ </tt></pre></div></div>
679
+ <div class="para"><p><span class="image">
680
+ <img src="images/polymorphic.png" alt="Polymorphic Association Diagram" title="Polymorphic Association Diagram" />
681
+ </span></p></div>
682
+ <h3 id="_self_joins">2.10. Self Joins</h3>
683
+ <div class="para"><p>In designing a data model, you will sometimes find a model that should have a relation to itself. For example, you may want to store all employees in a single database model, but be able to trace relationships such as manager and subordinates. This situation can be modeled with self-joining associations:</p></div>
684
+ <div class="listingblock">
685
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
686
+ by Lorenzo Bettini
687
+ http://www.lorenzobettini.it
688
+ http://www.gnu.org/software/src-highlite -->
689
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Employee <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
690
+ has_many <span style="color: #990000">:</span>subordinates<span style="color: #990000">,</span> <span style="color: #990000">:</span>class_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"User"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>foreign_key <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"manager_id"</span>
691
+ belongs_to <span style="color: #990000">:</span>manager<span style="color: #990000">,</span> <span style="color: #990000">:</span>class_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"User"</span>
692
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
693
+ </tt></pre></div></div>
694
+ <div class="para"><p>With this setup, you can retrieve <tt>@employee.subordinates</tt> and <tt>@employee.manager</tt>.</p></div>
695
+ </div>
696
+ <h2 id="_tips_tricks_and_warnings">3. Tips, Tricks, and Warnings</h2>
697
+ <div class="sectionbody">
698
+ <div class="para"><p>Here are a few things you should know to make efficient use of Active Record associations in your Rails applications:</p></div>
699
+ <div class="ilist"><ul>
700
+ <li>
701
+ <p>
702
+ Controlling caching
703
+ </p>
704
+ </li>
705
+ <li>
706
+ <p>
707
+ Avoiding name collisions
708
+ </p>
709
+ </li>
710
+ <li>
711
+ <p>
712
+ Updating the schema
713
+ </p>
714
+ </li>
715
+ <li>
716
+ <p>
717
+ Controlling association scope
718
+ </p>
719
+ </li>
720
+ </ul></div>
721
+ <h3 id="_controlling_caching">3.1. Controlling Caching</h3>
722
+ <div class="para"><p>All of the association methods are built around caching that keeps the result of the most recent query available for further operations. The cache is even shared across methods. For example:</p></div>
723
+ <div class="listingblock">
724
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
725
+ by Lorenzo Bettini
726
+ http://www.lorenzobettini.it
727
+ http://www.gnu.org/software/src-highlite -->
728
+ <pre><tt>customer<span style="color: #990000">.</span>orders <span style="font-style: italic"><span style="color: #9A1900"># retrieves orders from the database</span></span>
729
+ customer<span style="color: #990000">.</span>orders<span style="color: #990000">.</span>size <span style="font-style: italic"><span style="color: #9A1900"># uses the cached copy of orders</span></span>
730
+ customer<span style="color: #990000">.</span>orders<span style="color: #990000">.</span>empty? <span style="font-style: italic"><span style="color: #9A1900"># uses the cached copy of orders</span></span>
731
+ </tt></pre></div></div>
732
+ <div class="para"><p>But what if you want to reload the cache, because data might have been changed by some other part of the application? Just pass <tt>true</tt> to the association call:</p></div>
733
+ <div class="listingblock">
734
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
735
+ by Lorenzo Bettini
736
+ http://www.lorenzobettini.it
737
+ http://www.gnu.org/software/src-highlite -->
738
+ <pre><tt>customer<span style="color: #990000">.</span>orders <span style="font-style: italic"><span style="color: #9A1900"># retrieves orders from the database</span></span>
739
+ customer<span style="color: #990000">.</span>orders<span style="color: #990000">.</span>size <span style="font-style: italic"><span style="color: #9A1900"># uses the cached copy of orders</span></span>
740
+ customer<span style="color: #990000">.</span>orders<span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">).</span>empty? <span style="font-style: italic"><span style="color: #9A1900"># discards the cached copy of orders and goes back to the database</span></span>
741
+ </tt></pre></div></div>
742
+ <h3 id="_avoiding_name_collisions">3.2. Avoiding Name Collisions</h3>
743
+ <div class="para"><p>You are not free to use just any name for your associations. Because creating an association adds a method with that name to the model, it is a bad idea to give an association a name that is already used for an instance method of <tt>ActiveRecord::Base</tt>. The association method would override the base method and break things. For instance, <tt>attributes</tt> or <tt>connection</tt> are bad names for associations.</p></div>
744
+ <h3 id="_updating_the_schema">3.3. Updating the Schema</h3>
745
+ <div class="para"><p>Associations are extremely useful, but they are not magic. You are responsible for maintaining your database schema to match your associations. In practice, this means two things. First, you need to create foreign keys as appropriate:</p></div>
746
+ <div class="listingblock">
747
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
748
+ by Lorenzo Bettini
749
+ http://www.lorenzobettini.it
750
+ http://www.gnu.org/software/src-highlite -->
751
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Order <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
752
+ belongs_to <span style="color: #990000">:</span>customer
753
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
754
+ </tt></pre></div></div>
755
+ <div class="para"><p>This declaration needs to be backed up by the proper foreign key declaration on the orders table:</p></div>
756
+ <div class="listingblock">
757
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
758
+ by Lorenzo Bettini
759
+ http://www.lorenzobettini.it
760
+ http://www.gnu.org/software/src-highlite -->
761
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> CreateOrders <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Migration
762
+ <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>up
763
+ create_table <span style="color: #990000">:</span>orders <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>t<span style="color: #990000">|</span>
764
+ t<span style="color: #990000">.</span>order_date <span style="color: #990000">:</span>datetime
765
+ t<span style="color: #990000">.</span>order_number <span style="color: #990000">:</span>string
766
+ t<span style="color: #990000">.</span>customer_id <span style="color: #990000">:</span>integer
767
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
768
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
769
+
770
+ <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>down
771
+ drop_table <span style="color: #990000">:</span>orders
772
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
773
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
774
+ </tt></pre></div></div>
775
+ <div class="para"><p>If you create an association some time after you build the underlying model, you need to remember to create an <tt>add_column</tt> migration to provide the necessary foreign key.</p></div>
776
+ <div class="para"><p>Second, if you create a <tt>has_and_belongs_to_many</tt> association, you need to explicitly create the joining table. Unless the name of the join table is explicitly specified by using the <tt>:join_table</tt> option, Active Record create the name by using the lexical order of the class names. So a join between customer and order models will give the default join table name of "customers_orders" because "c" outranks "o" in lexical ordering.</p></div>
777
+ <div class="admonitionblock">
778
+ <table><tr>
779
+ <td class="icon">
780
+ <img src="./images/icons/warning.png" alt="Warning" />
781
+ </td>
782
+ <td class="content">The precedence between model names is calculated using the <tt>&lt;</tt> operator for <tt>String</tt>. This means that if the strings are of different lengths, and the strings are equal when compared up to the shortest length, then the longer string is considered of higher lexical precedence than the shorter one. For example, one would expect the tables "paper_boxes" and "papers" to generate a join table name of "papers_paper_boxes" because of the length of the name "paper_boxes", but it in fact generates a join table name of "paper_boxes_papers".</td>
783
+ </tr></table>
784
+ </div>
785
+ <div class="para"><p>Whatever the name, you must manually generate the join table with an appropriate migration. For example, consider these associations:</p></div>
786
+ <div class="listingblock">
787
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
788
+ by Lorenzo Bettini
789
+ http://www.lorenzobettini.it
790
+ http://www.gnu.org/software/src-highlite -->
791
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Assembly <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
792
+ has_and_belongs_to_many <span style="color: #990000">:</span>parts
793
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
794
+
795
+ <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Part <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
796
+ has_and_belongs_to_many <span style="color: #990000">:</span>assemblies
797
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
798
+ </tt></pre></div></div>
799
+ <div class="para"><p>These need to be backed up by a migration to create the <tt>assemblies_parts</tt> table. This table should be created without a primary key:</p></div>
800
+ <div class="listingblock">
801
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
802
+ by Lorenzo Bettini
803
+ http://www.lorenzobettini.it
804
+ http://www.gnu.org/software/src-highlite -->
805
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> CreateAssemblyPartJoinTable <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Migration
806
+ <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>up
807
+ create_table <span style="color: #990000">:</span>assemblies_parts<span style="color: #990000">,</span> <span style="color: #990000">:</span>id <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>t<span style="color: #990000">|</span>
808
+ t<span style="color: #990000">.</span>integer <span style="color: #990000">:</span>assembly_id
809
+ t<span style="color: #990000">.</span>integer <span style="color: #990000">:</span>part_id
810
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
811
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
812
+
813
+ <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>down
814
+ drop_table <span style="color: #990000">:</span>assemblies_parts
815
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
816
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
817
+ </tt></pre></div></div>
818
+ <h3 id="_controlling_association_scope">3.4. Controlling Association Scope</h3>
819
+ <div class="para"><p>By default, associations look for objects only within the current module's scope. This can be important when you declare Active Record models within a module. For example:</p></div>
820
+ <div class="listingblock">
821
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
822
+ by Lorenzo Bettini
823
+ http://www.lorenzobettini.it
824
+ http://www.gnu.org/software/src-highlite -->
825
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">module</span></span> MyApplication
826
+ <span style="font-weight: bold"><span style="color: #0000FF">module</span></span> Business
827
+ <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Supplier <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
828
+ has_one <span style="color: #990000">:</span>account
829
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
830
+
831
+ <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Account <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
832
+ belongs_to <span style="color: #990000">:</span>supplier
833
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
834
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></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 will work fine, because both the <tt>Supplier</tt> and the <tt>Account</tt> class are defined within the same scope. But this will not work, because <tt>Supplier</tt> and <tt>Account</tt> are defined in different scopes:</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">module</span></span> MyApplication
844
+ <span style="font-weight: bold"><span style="color: #0000FF">module</span></span> Business
845
+ <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Supplier <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
846
+ has_one <span style="color: #990000">:</span>account
847
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
848
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
849
+
850
+ <span style="font-weight: bold"><span style="color: #0000FF">module</span></span> Billing
851
+ <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Account <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
852
+ belongs_to <span style="color: #990000">:</span>supplier
853
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
854
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
855
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
856
+ </tt></pre></div></div>
857
+ <div class="para"><p>To associate a model with a model in a different scope, you must specify the complete class name in your association declaration:</p></div>
858
+ <div class="listingblock">
859
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
860
+ by Lorenzo Bettini
861
+ http://www.lorenzobettini.it
862
+ http://www.gnu.org/software/src-highlite -->
863
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">module</span></span> MyApplication
864
+ <span style="font-weight: bold"><span style="color: #0000FF">module</span></span> Business
865
+ <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Supplier <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
866
+ has_one <span style="color: #990000">:</span>account<span style="color: #990000">,</span> <span style="color: #990000">:</span>class_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"MyApplication::Billing::Account"</span>
867
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
868
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
869
+
870
+ <span style="font-weight: bold"><span style="color: #0000FF">module</span></span> Billing
871
+ <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Account <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
872
+ belongs_to <span style="color: #990000">:</span>supplier<span style="color: #990000">,</span> <span style="color: #990000">:</span>class_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"MyApplication::Business::Supplier"</span>
873
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
874
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
875
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
876
+ </tt></pre></div></div>
877
+ </div>
878
+ <h2 id="_detailed_association_reference">4. Detailed Association Reference</h2>
879
+ <div class="sectionbody">
880
+ <div class="para"><p>The following sections give the details of each type of association, including the methods that they add and the options that you can use when declaring an association.</p></div>
881
+ <h3 id="_the_tt_belongs_to_tt_association_2">4.1. The <tt>belongs_to</tt> Association</h3>
882
+ <div class="para"><p>The <tt>belongs_to</tt> association creates a one-to-one match with another model. In database terms, this association says that this class contains the foreign key. If the other class contains the foreign key, then you should use <tt>has_one</tt> instead.</p></div>
883
+ <h4 id="_methods_added_by_tt_belongs_to_tt">4.1.1. Methods Added by <tt>belongs_to</tt></h4>
884
+ <div class="para"><p>When you declare a <tt>belongs_to</tt> assocation, the declaring class automatically gains five methods related to the association:</p></div>
885
+ <div class="ilist"><ul>
886
+ <li>
887
+ <p>
888
+ <tt><em>association</em>(force_reload = false)</tt>
889
+ </p>
890
+ </li>
891
+ <li>
892
+ <p>
893
+ <tt><em>association</em>=(associate)</tt>
894
+ </p>
895
+ </li>
896
+ <li>
897
+ <p>
898
+ <tt><em>association</em>.nil?</tt>
899
+ </p>
900
+ </li>
901
+ <li>
902
+ <p>
903
+ <tt>build<em>_association</em>(attributes = {})</tt>
904
+ </p>
905
+ </li>
906
+ <li>
907
+ <p>
908
+ <tt>create<em>_association</em>(attributes = {})</tt>
909
+ </p>
910
+ </li>
911
+ </ul></div>
912
+ <div class="para"><p>In all of these methods, <tt><em>association</em></tt> is replaced with the symbol passed as the first argument to <tt>belongs_to</tt>. For example, given the declaration:</p></div>
913
+ <div class="listingblock">
914
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
915
+ by Lorenzo Bettini
916
+ http://www.lorenzobettini.it
917
+ http://www.gnu.org/software/src-highlite -->
918
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Order <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
919
+ belongs_to <span style="color: #990000">:</span>customer
920
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
921
+ </tt></pre></div></div>
922
+ <div class="para"><p>Each instance of the order model will have these methods:</p></div>
923
+ <div class="listingblock">
924
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
925
+ by Lorenzo Bettini
926
+ http://www.lorenzobettini.it
927
+ http://www.gnu.org/software/src-highlite -->
928
+ <pre><tt>customer
929
+ customer<span style="color: #990000">=</span>
930
+ customer<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #0000FF">nil</span></span><span style="color: #990000">?</span>
931
+ build_customer
932
+ create_customer
933
+ </tt></pre></div></div>
934
+ <h5 id="_tt_em_association_em_force_reload_false_tt"><tt><em>association</em>(force_reload = false)</tt></h5>
935
+ <div class="para"><p>The <tt><em>association</em></tt> method returns the associated object, if any. If no associated object is found, it returns <tt>nil</tt>.</p></div>
936
+ <div class="listingblock">
937
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
938
+ by Lorenzo Bettini
939
+ http://www.lorenzobettini.it
940
+ http://www.gnu.org/software/src-highlite -->
941
+ <pre><tt><span style="color: #009900">@customer</span> <span style="color: #990000">=</span> <span style="color: #009900">@order</span><span style="color: #990000">.</span>customer
942
+ </tt></pre></div></div>
943
+ <div class="para"><p>If the associated object has already been retrieved from the database for this object, the cached version will be returned. To override this behavior (and force a database read), pass <tt>true</tt> as the <tt>force_reload</tt> argument.</p></div>
944
+ <h5 id="_tt_em_association_em_associate_tt"><tt><em>association</em>=(associate)</tt></h5>
945
+ <div class="para"><p>The <tt><em>association</em>=</tt> method assigns an associated object to this object. Behind the scenes, this means extracting the primary key from the associate object and setting this object's foreign key to the same value.</p></div>
946
+ <div class="listingblock">
947
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
948
+ by Lorenzo Bettini
949
+ http://www.lorenzobettini.it
950
+ http://www.gnu.org/software/src-highlite -->
951
+ <pre><tt><span style="color: #009900">@order</span><span style="color: #990000">.</span>customer <span style="color: #990000">=</span> <span style="color: #009900">@customer</span>
952
+ </tt></pre></div></div>
953
+ <h5 id="_tt_em_association_em_nil_tt"><tt><em>association</em>.nil?</tt></h5>
954
+ <div class="para"><p>The <tt><em>association</em>.nil?</tt> method returns <tt>true</tt> if there is no associated object.</p></div>
955
+ <div class="listingblock">
956
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
957
+ by Lorenzo Bettini
958
+ http://www.lorenzobettini.it
959
+ http://www.gnu.org/software/src-highlite -->
960
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #009900">@order</span><span style="color: #990000">.</span>customer<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #0000FF">nil</span></span><span style="color: #990000">?</span>
961
+ <span style="color: #009900">@msg</span> <span style="color: #990000">=</span> <span style="color: #FF0000">"No customer found for this order"</span>
962
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
963
+ </tt></pre></div></div>
964
+ <h5 id="_tt_build_em_association_em_attributes_tt"><tt>build<em>_association</em>(attributes = {})</tt></h5>
965
+ <div class="para"><p>The <tt>build<em>_association</em></tt> method returns a new object of the associated type. This object will be instantiated from the passed attributes, and the link through this object's foreign key will be set, but the associated object will _not_ yet be saved.</p></div>
966
+ <div class="listingblock">
967
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
968
+ by Lorenzo Bettini
969
+ http://www.lorenzobettini.it
970
+ http://www.gnu.org/software/src-highlite -->
971
+ <pre><tt><span style="color: #009900">@customer</span> <span style="color: #990000">=</span> <span style="color: #009900">@order</span><span style="color: #990000">.</span>build_customer<span style="color: #990000">(</span><span style="color: #FF0000">{</span><span style="color: #990000">:</span>customer_number <span style="color: #990000">=&gt;</span> <span style="color: #993399">123</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>customer_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"John Doe"</span><span style="color: #FF0000">}</span><span style="color: #990000">)</span>
972
+ </tt></pre></div></div>
973
+ <h5 id="_tt_create_em_association_em_attributes_tt"><tt>create<em>_association</em>(attributes = {})</tt></h5>
974
+ <div class="para"><p>The <tt>create<em>_association</em></tt> method returns a new object of the associated type. This object will be instantiated from the passed attributes, and the link through this object's foreign key will be set. In addition, the associated object _will_ be saved (assuming that it passes any validations).</p></div>
975
+ <div class="listingblock">
976
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
977
+ by Lorenzo Bettini
978
+ http://www.lorenzobettini.it
979
+ http://www.gnu.org/software/src-highlite -->
980
+ <pre><tt><span style="color: #009900">@customer</span> <span style="color: #990000">=</span> <span style="color: #009900">@order</span><span style="color: #990000">.</span>create_customer<span style="color: #990000">(</span><span style="color: #FF0000">{</span><span style="color: #990000">:</span>customer_number <span style="color: #990000">=&gt;</span> <span style="color: #993399">123</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>customer_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"John Doe"</span><span style="color: #FF0000">}</span><span style="color: #990000">)</span>
981
+ </tt></pre></div></div>
982
+ <h4 id="_options_for_tt_belongs_to_tt">4.1.2. Options for <tt>belongs_to</tt></h4>
983
+ <div class="para"><p>In many situations, you can use the default behavior of <tt>belongs_to</tt> without any customization. But despite Rails' emphasis of convention over customization, you can alter that behavior in a number of ways. This section covers the options that you can pass when you create a <tt>belongs_to</tt> association. For example, an association with several options might look like this:</p></div>
984
+ <div class="listingblock">
985
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
986
+ by Lorenzo Bettini
987
+ http://www.lorenzobettini.it
988
+ http://www.gnu.org/software/src-highlite -->
989
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Order <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
990
+ belongs_to <span style="color: #990000">:</span>customer<span style="color: #990000">,</span> <span style="color: #990000">:</span>counter_cache <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"active = 1"</span>
991
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
992
+ </tt></pre></div></div>
993
+ <div class="para"><p>The <tt>belongs_to</tt> association supports these options:</p></div>
994
+ <div class="ilist"><ul>
995
+ <li>
996
+ <p>
997
+ <tt>:class_name</tt>
998
+ </p>
999
+ </li>
1000
+ <li>
1001
+ <p>
1002
+ <tt>:conditions</tt>
1003
+ </p>
1004
+ </li>
1005
+ <li>
1006
+ <p>
1007
+ <tt>:counter_cache</tt>
1008
+ </p>
1009
+ </li>
1010
+ <li>
1011
+ <p>
1012
+ <tt>:dependent</tt>
1013
+ </p>
1014
+ </li>
1015
+ <li>
1016
+ <p>
1017
+ <tt>:foreign_key</tt>
1018
+ </p>
1019
+ </li>
1020
+ <li>
1021
+ <p>
1022
+ <tt>:include</tt>
1023
+ </p>
1024
+ </li>
1025
+ <li>
1026
+ <p>
1027
+ <tt>:polymorphic</tt>
1028
+ </p>
1029
+ </li>
1030
+ <li>
1031
+ <p>
1032
+ <tt>:readonly</tt>
1033
+ </p>
1034
+ </li>
1035
+ <li>
1036
+ <p>
1037
+ <tt>:select</tt>
1038
+ </p>
1039
+ </li>
1040
+ <li>
1041
+ <p>
1042
+ <tt>:validate</tt>
1043
+ </p>
1044
+ </li>
1045
+ </ul></div>
1046
+ <h5 id="_tt_class_name_tt"><tt>:class_name</tt></h5>
1047
+ <div class="para"><p>If the name of the other model cannot be derived from the association name, you can use the <tt>:class_name</tt> option to supply the model name. For example, if an order belongs to a customer, but the actual name of the model containing customers is <tt>Patron</tt>, you'd set things up this way:</p></div>
1048
+ <div class="listingblock">
1049
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
1050
+ by Lorenzo Bettini
1051
+ http://www.lorenzobettini.it
1052
+ http://www.gnu.org/software/src-highlite -->
1053
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Order <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
1054
+ belongs_to <span style="color: #990000">:</span>customer<span style="color: #990000">,</span> <span style="color: #990000">:</span>class_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Patron"</span>
1055
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1056
+ </tt></pre></div></div>
1057
+ <h5 id="_tt_conditions_tt"><tt>:conditions</tt></h5>
1058
+ <div class="para"><p>The <tt>:conditions</tt> option lets you specify the conditions that the associated object must meet (in the syntax used by a SQL <tt>WHERE</tt> clause).</p></div>
1059
+ <div class="listingblock">
1060
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
1061
+ by Lorenzo Bettini
1062
+ http://www.lorenzobettini.it
1063
+ http://www.gnu.org/software/src-highlite -->
1064
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Order <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
1065
+ belongs_to <span style="color: #990000">:</span>customer<span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"active = 1"</span>
1066
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1067
+ </tt></pre></div></div>
1068
+ <h5 id="_tt_counter_cache_tt"><tt>:counter_cache</tt></h5>
1069
+ <div class="para"><p>The <tt>:counter_cache</tt> option can be used to make finding the number of belonging objects more efficient. Consider these models:</p></div>
1070
+ <div class="listingblock">
1071
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
1072
+ by Lorenzo Bettini
1073
+ http://www.lorenzobettini.it
1074
+ http://www.gnu.org/software/src-highlite -->
1075
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Order <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
1076
+ belongs_to <span style="color: #990000">:</span>customer
1077
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1078
+ <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
1079
+ has_many <span style="color: #990000">:</span>orders
1080
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1081
+ </tt></pre></div></div>
1082
+ <div class="para"><p>With these declarations, asking for the value of <tt>@customer.orders.size</tt> requires making a call to the database to perform a <tt>COUNT(*)</tt> query. To avoid this call, you can add a counter cache to the <em>belonging</em> model:</p></div>
1083
+ <div class="listingblock">
1084
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
1085
+ by Lorenzo Bettini
1086
+ http://www.lorenzobettini.it
1087
+ http://www.gnu.org/software/src-highlite -->
1088
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Order <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
1089
+ belongs_to <span style="color: #990000">:</span>customer<span style="color: #990000">,</span> <span style="color: #990000">:</span>counter_cache <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
1090
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1091
+ <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
1092
+ has_many <span style="color: #990000">:</span>orders
1093
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1094
+ </tt></pre></div></div>
1095
+ <div class="para"><p>With this declaration, Rails will keep the cache value up to date, and then return that value in response to the <tt>.size</tt> method.</p></div>
1096
+ <div class="para"><p>Although the <tt>:counter_cache</tt> option is specified on the model that includes the <tt>belongs_to</tt> declaration, the actual column must be added to the <em>associated</em> model. In the case above, you would need to add a column named <tt>orders_count</tt> to the <tt>Customer</tt> model. You can override the default column name if you need to:</p></div>
1097
+ <div class="listingblock">
1098
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
1099
+ by Lorenzo Bettini
1100
+ http://www.lorenzobettini.it
1101
+ http://www.gnu.org/software/src-highlite -->
1102
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Order <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
1103
+ belongs_to <span style="color: #990000">:</span>customer<span style="color: #990000">,</span> <span style="color: #990000">:</span>counter_cache <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>count_of_orders
1104
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1105
+ <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
1106
+ has_many <span style="color: #990000">:</span>orders
1107
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1108
+ </tt></pre></div></div>
1109
+ <div class="para"><p>Counter cache columns are added to the containing model's list of read-only attributes through <tt>attr_readonly</tt>.</p></div>
1110
+ <h5 id="_tt_dependent_tt"><tt>:dependent</tt></h5>
1111
+ <div class="para"><p>If you set the <tt>:dependent</tt> option to <tt>:destroy</tt>, then deleting this object will call the destroy method on the associated object to delete that object. If you set the <tt>:dependent</tt> option to <tt>:delete</tt>, then deleting this object will delete the associated object <em>without</em> calling its <tt>destroy</tt> method.</p></div>
1112
+ <div class="admonitionblock">
1113
+ <table><tr>
1114
+ <td class="icon">
1115
+ <img src="./images/icons/warning.png" alt="Warning" />
1116
+ </td>
1117
+ <td class="content">You should not specify this option on a <tt>belongs_to</tt> association that is connected with a <tt>has_many</tt> association on the other class. Doing so can lead to orphaned records in your database.</td>
1118
+ </tr></table>
1119
+ </div>
1120
+ <h5 id="_tt_foreign_key_tt"><tt>:foreign_key</tt></h5>
1121
+ <div class="para"><p>By convention, Rails guesses that the column used to hold the foreign key on this model is the name of the association with the suffix <tt>_id</tt> added. The <tt>:foreign_key</tt> option lets you set the name of the foreign key directly:</p></div>
1122
+ <div class="listingblock">
1123
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
1124
+ by Lorenzo Bettini
1125
+ http://www.lorenzobettini.it
1126
+ http://www.gnu.org/software/src-highlite -->
1127
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Order <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
1128
+ belongs_to <span style="color: #990000">:</span>customer<span style="color: #990000">,</span> <span style="color: #990000">:</span>class_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Patron"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>foreign_key <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"patron_id"</span>
1129
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1130
+ </tt></pre></div></div>
1131
+ <div class="admonitionblock">
1132
+ <table><tr>
1133
+ <td class="icon">
1134
+ <img src="./images/icons/tip.png" alt="Tip" />
1135
+ </td>
1136
+ <td class="content">In any case, Rails will not create foreign key columns for you. You need to explicitly define them as part of your migrations.</td>
1137
+ </tr></table>
1138
+ </div>
1139
+ <h5 id="_tt_include_tt"><tt>:include</tt></h5>
1140
+ <div class="para"><p>You can use the :include option to specify second-order associations that should be eager-loaded when this association is used. For example, consider these models:</p></div>
1141
+ <div class="listingblock">
1142
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
1143
+ by Lorenzo Bettini
1144
+ http://www.lorenzobettini.it
1145
+ http://www.gnu.org/software/src-highlite -->
1146
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> LineItem <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
1147
+ belongs_to <span style="color: #990000">:</span>order
1148
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1149
+ <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Order <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
1150
+ belongs_to <span style="color: #990000">:</span>customer
1151
+ has_many <span style="color: #990000">:</span>line_items
1152
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1153
+ <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
1154
+ has_many <span style="color: #990000">:</span>orders
1155
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1156
+ </tt></pre></div></div>
1157
+ <div class="para"><p>If you frequently retrieve customers directly from line items (<tt>@line_item.order.customer</tt>), then you can make your code somewhat more efficient by including customers in the association from line items to orders:</p></div>
1158
+ <div class="listingblock">
1159
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
1160
+ by Lorenzo Bettini
1161
+ http://www.lorenzobettini.it
1162
+ http://www.gnu.org/software/src-highlite -->
1163
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> LineItem <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
1164
+ belongs_to <span style="color: #990000">:</span>order<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">=&gt;</span> <span style="color: #990000">:</span>customer
1165
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1166
+ <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Order <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
1167
+ belongs_to <span style="color: #990000">:</span>customer
1168
+ has_many <span style="color: #990000">:</span>line_items
1169
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1170
+ <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
1171
+ has_many <span style="color: #990000">:</span>orders
1172
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1173
+ </tt></pre></div></div>
1174
+ <div class="admonitionblock">
1175
+ <table><tr>
1176
+ <td class="icon">
1177
+ <img src="./images/icons/note.png" alt="Note" />
1178
+ </td>
1179
+ <td class="content">There's no need to use <tt>:include</tt> for immediate associations - that is, if you have <tt>Order belongs_to :customer</tt>, then the customer is eager-loaded automatically when it's needed.</td>
1180
+ </tr></table>
1181
+ </div>
1182
+ <h5 id="_tt_polymorphic_tt"><tt>:polymorphic</tt></h5>
1183
+ <div class="para"><p>Passing <tt>true</tt> to the <tt>:polymorphic</tt> option indicates that this is a polymorphic association. Polymorphic associations were discussed in detail earlier in this guide.</p></div>
1184
+ <h5 id="_tt_readonly_tt"><tt>:readonly</tt></h5>
1185
+ <div class="para"><p>If you set the <tt>:readonly</tt> option to <tt>true</tt>, then the associated object will be read-only when retrieved via the association.</p></div>
1186
+ <h5 id="_tt_select_tt"><tt>:select</tt></h5>
1187
+ <div class="para"><p>The <tt>:select</tt> option lets you override the SQL <tt>SELECT</tt> clause that is used to retrieve data about the associated object. By default, Rails retrieves all columns.</p></div>
1188
+ <div class="admonitionblock">
1189
+ <table><tr>
1190
+ <td class="icon">
1191
+ <img src="./images/icons/tip.png" alt="Tip" />
1192
+ </td>
1193
+ <td class="content">If you set the <tt>:select</tt> option on a <tt>belongs_to</tt> association, you should also set the <tt>foreign_key</tt> option to guarantee the correct results.</td>
1194
+ </tr></table>
1195
+ </div>
1196
+ <h5 id="_tt_validate_tt"><tt>:validate</tt></h5>
1197
+ <div class="para"><p>If you set the <tt>:validate</tt> option to <tt>true</tt>, then associated objects will be validated whenever you save this object. By default, this is <tt>false</tt>: associated objects will not be validated when this object is saved.</p></div>
1198
+ <h4 id="_when_are_objects_saved">4.1.3. When are Objects Saved?</h4>
1199
+ <div class="para"><p>Assigning an object to a <tt>belongs_to</tt> association does <em>not</em> automatically save the object. It does not save the associated object either.</p></div>
1200
+ <h3 id="_the_has_one_association">4.2. The has_one Association</h3>
1201
+ <div class="para"><p>The <tt>has_one</tt> association creates a one-to-one match with another model. In database terms, this association says that the other class contains the foreign key. If this class contains the foreign key, then you should use <tt>belongs_to</tt> instead.</p></div>
1202
+ <h4 id="_methods_added_by_tt_has_one_tt">4.2.1. Methods Added by <tt>has_one</tt></h4>
1203
+ <div class="para"><p>When you declare a <tt>has_one</tt> association, the declaring class automatically gains five methods related to the association:</p></div>
1204
+ <div class="ilist"><ul>
1205
+ <li>
1206
+ <p>
1207
+ <tt><em>association</em>(force_reload = false)</tt>
1208
+ </p>
1209
+ </li>
1210
+ <li>
1211
+ <p>
1212
+ <tt><em>association</em>=(associate)</tt>
1213
+ </p>
1214
+ </li>
1215
+ <li>
1216
+ <p>
1217
+ <tt><em>association</em>.nil?</tt>
1218
+ </p>
1219
+ </li>
1220
+ <li>
1221
+ <p>
1222
+ <tt>build<em>_association</em>(attributes = {})</tt>
1223
+ </p>
1224
+ </li>
1225
+ <li>
1226
+ <p>
1227
+ <tt>create<em>_association</em>(attributes = {})</tt>
1228
+ </p>
1229
+ </li>
1230
+ </ul></div>
1231
+ <div class="para"><p>In all of these methods, <tt><em>association</em></tt> is replaced with the symbol passed as the first argument to <tt>has_one</tt>. For example, given the declaration:</p></div>
1232
+ <div class="listingblock">
1233
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
1234
+ by Lorenzo Bettini
1235
+ http://www.lorenzobettini.it
1236
+ http://www.gnu.org/software/src-highlite -->
1237
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Supplier <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
1238
+ has_one <span style="color: #990000">:</span>account
1239
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1240
+ </tt></pre></div></div>
1241
+ <div class="para"><p>Each instance of the <tt>Supplier</tt> model will have these methods:</p></div>
1242
+ <div class="listingblock">
1243
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
1244
+ by Lorenzo Bettini
1245
+ http://www.lorenzobettini.it
1246
+ http://www.gnu.org/software/src-highlite -->
1247
+ <pre><tt>account
1248
+ account<span style="color: #990000">=</span>
1249
+ account<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #0000FF">nil</span></span><span style="color: #990000">?</span>
1250
+ build_account
1251
+ create_account
1252
+ </tt></pre></div></div>
1253
+ <h5 id="_tt_em_association_em_force_reload_false_tt_2"><tt><em>association</em>(force_reload = false)</tt></h5>
1254
+ <div class="para"><p>The <tt><em>association</em></tt> method returns the associated object, if any. If no associated object is found, it returns <tt>nil</tt>.</p></div>
1255
+ <div class="listingblock">
1256
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
1257
+ by Lorenzo Bettini
1258
+ http://www.lorenzobettini.it
1259
+ http://www.gnu.org/software/src-highlite -->
1260
+ <pre><tt><span style="color: #009900">@account</span> <span style="color: #990000">=</span> <span style="color: #009900">@supplier</span><span style="color: #990000">.</span>account
1261
+ </tt></pre></div></div>
1262
+ <div class="para"><p>If the associated object has already been retrieved from the database for this object, the cached version will be returned. To override this behavior (and force a database read), pass <tt>true</tt> as the <tt>force_reload</tt> argument.</p></div>
1263
+ <h5 id="_tt_em_association_em_associate_tt_2"><tt><em>association</em>=(associate)</tt></h5>
1264
+ <div class="para"><p>The <tt><em>association</em>=</tt> method assigns an associated object to this object. Behind the scenes, this means extracting the primary key from this object and setting the associate object's foreign key to the same value.</p></div>
1265
+ <div class="listingblock">
1266
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
1267
+ by Lorenzo Bettini
1268
+ http://www.lorenzobettini.it
1269
+ http://www.gnu.org/software/src-highlite -->
1270
+ <pre><tt><span style="color: #009900">@suppler</span><span style="color: #990000">.</span>account <span style="color: #990000">=</span> <span style="color: #009900">@account</span>
1271
+ </tt></pre></div></div>
1272
+ <h5 id="_tt_em_association_em_nil_tt_2"><tt><em>association</em>.nil?</tt></h5>
1273
+ <div class="para"><p>The <tt><em>association</em>.nil?</tt> method returns <tt>true</tt> if there is no associated object.</p></div>
1274
+ <div class="listingblock">
1275
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
1276
+ by Lorenzo Bettini
1277
+ http://www.lorenzobettini.it
1278
+ http://www.gnu.org/software/src-highlite -->
1279
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #009900">@supplier</span><span style="color: #990000">.</span>account<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #0000FF">nil</span></span><span style="color: #990000">?</span>
1280
+ <span style="color: #009900">@msg</span> <span style="color: #990000">=</span> <span style="color: #FF0000">"No account found for this supplier"</span>
1281
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1282
+ </tt></pre></div></div>
1283
+ <h5 id="_tt_build_em_association_em_attributes_tt_2"><tt>build<em>_association</em>(attributes = {})</tt></h5>
1284
+ <div class="para"><p>The <tt>build<em>_association</em></tt> method returns a new object of the associated type. This object will be instantiated from the passed attributes, and the link through its foreign key will be set, but the associated object will _not_ yet be saved.</p></div>
1285
+ <div class="listingblock">
1286
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
1287
+ by Lorenzo Bettini
1288
+ http://www.lorenzobettini.it
1289
+ http://www.gnu.org/software/src-highlite -->
1290
+ <pre><tt><span style="color: #009900">@account</span> <span style="color: #990000">=</span> <span style="color: #009900">@supplier</span><span style="color: #990000">.</span>build_account<span style="color: #990000">(</span><span style="color: #FF0000">{</span><span style="color: #990000">:</span>terms <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Net 30"</span><span style="color: #FF0000">}</span><span style="color: #990000">)</span>
1291
+ </tt></pre></div></div>
1292
+ <h5 id="_tt_create_em_association_em_attributes_tt_2"><tt>create<em>_association</em>(attributes = {})</tt></h5>
1293
+ <div class="para"><p>The <tt>create<em>_association</em></tt> method returns a new object of the associated type. This object will be instantiated from the passed attributes, and the link through its foreign key will be set. In addition, the associated object _will_ be saved (assuming that it passes any validations).</p></div>
1294
+ <div class="listingblock">
1295
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
1296
+ by Lorenzo Bettini
1297
+ http://www.lorenzobettini.it
1298
+ http://www.gnu.org/software/src-highlite -->
1299
+ <pre><tt><span style="color: #009900">@account</span> <span style="color: #990000">=</span> <span style="color: #009900">@supplier</span><span style="color: #990000">.</span>create_account<span style="color: #990000">(</span><span style="color: #FF0000">{</span><span style="color: #990000">:</span>terms <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Net 30"</span><span style="color: #FF0000">}</span><span style="color: #990000">)</span>
1300
+ </tt></pre></div></div>
1301
+ <h4 id="_options_for_tt_has_one_tt">4.2.2. Options for <tt>has_one</tt></h4>
1302
+ <div class="para"><p>In many situations, you can use the default behavior of <tt>has_one</tt> without any customization. But despite Rails' emphasis of convention over customization, you can alter that behavior in a number of ways. This section covers the options that you can pass when you create a <tt>has_one</tt> association. For example, an association with several options might look like this:</p></div>
1303
+ <div class="listingblock">
1304
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
1305
+ by Lorenzo Bettini
1306
+ http://www.lorenzobettini.it
1307
+ http://www.gnu.org/software/src-highlite -->
1308
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Supplier <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
1309
+ has_one <span style="color: #990000">:</span>account<span style="color: #990000">,</span> <span style="color: #990000">:</span>class_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Billing"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>dependent <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>nullify
1310
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1311
+ </tt></pre></div></div>
1312
+ <div class="para"><p>The <tt>has_one</tt> association supports these options:</p></div>
1313
+ <div class="ilist"><ul>
1314
+ <li>
1315
+ <p>
1316
+ <tt>:as</tt>
1317
+ </p>
1318
+ </li>
1319
+ <li>
1320
+ <p>
1321
+ <tt>:class_name</tt>
1322
+ </p>
1323
+ </li>
1324
+ <li>
1325
+ <p>
1326
+ <tt>:conditions</tt>
1327
+ </p>
1328
+ </li>
1329
+ <li>
1330
+ <p>
1331
+ <tt>:dependent</tt>
1332
+ </p>
1333
+ </li>
1334
+ <li>
1335
+ <p>
1336
+ <tt>:foreign_key</tt>
1337
+ </p>
1338
+ </li>
1339
+ <li>
1340
+ <p>
1341
+ <tt>:include</tt>
1342
+ </p>
1343
+ </li>
1344
+ <li>
1345
+ <p>
1346
+ <tt>:order</tt>
1347
+ </p>
1348
+ </li>
1349
+ <li>
1350
+ <p>
1351
+ <tt>:primary_key</tt>
1352
+ </p>
1353
+ </li>
1354
+ <li>
1355
+ <p>
1356
+ <tt>:readonly</tt>
1357
+ </p>
1358
+ </li>
1359
+ <li>
1360
+ <p>
1361
+ <tt>:select</tt>
1362
+ </p>
1363
+ </li>
1364
+ <li>
1365
+ <p>
1366
+ <tt>:source</tt>
1367
+ </p>
1368
+ </li>
1369
+ <li>
1370
+ <p>
1371
+ <tt>:source_type</tt>
1372
+ </p>
1373
+ </li>
1374
+ <li>
1375
+ <p>
1376
+ <tt>:through</tt>
1377
+ </p>
1378
+ </li>
1379
+ <li>
1380
+ <p>
1381
+ <tt>:validate</tt>
1382
+ </p>
1383
+ </li>
1384
+ </ul></div>
1385
+ <h5 id="_tt_as_tt"><tt>:as</tt></h5>
1386
+ <div class="para"><p>Setting the <tt>:as</tt> option indicates that this is a polymorphic association. Polymorphic associations are discussed in detail later in this guide.</p></div>
1387
+ <h5 id="_tt_class_name_tt_2"><tt>:class_name</tt></h5>
1388
+ <div class="para"><p>If the name of the other model cannot be derived from the association name, you can use the <tt>:class_name</tt> option to supply the model name. For example, if a supplier has an account, but the actual name of the model containing accounts is Billing, you'd set things up this way:</p></div>
1389
+ <div class="listingblock">
1390
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
1391
+ by Lorenzo Bettini
1392
+ http://www.lorenzobettini.it
1393
+ http://www.gnu.org/software/src-highlite -->
1394
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Supplier <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
1395
+ has_one <span style="color: #990000">:</span>account<span style="color: #990000">,</span> <span style="color: #990000">:</span>class_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Billing"</span>
1396
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1397
+ </tt></pre></div></div>
1398
+ <h5 id="_tt_conditions_tt_2"><tt>:conditions</tt></h5>
1399
+ <div class="para"><p>The <tt>:conditions</tt> option lets you specify the conditions that the associated object must meet (in the syntax used by a SQL <tt>WHERE</tt> clause).</p></div>
1400
+ <div class="listingblock">
1401
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
1402
+ by Lorenzo Bettini
1403
+ http://www.lorenzobettini.it
1404
+ http://www.gnu.org/software/src-highlite -->
1405
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Supplier <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
1406
+ has_one <span style="color: #990000">:</span>account<span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"confirmed = 1"</span>
1407
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1408
+ </tt></pre></div></div>
1409
+ <h5 id="_tt_dependent_tt_2"><tt>:dependent</tt></h5>
1410
+ <div class="para"><p>If you set the <tt>:dependent</tt> option to <tt>:destroy</tt>, then deleting this object will call the destroy method on the associated object to delete that object. If you set the <tt>:dependent</tt> option to <tt>:delete</tt>, then deleting this object will delete the associated object <em>without</em> calling its <tt>destroy</tt> method. If you set the <tt>:dependent</tt> option to <tt>:nullify</tt>, then deleting this object will set the foreign key in the association object to <tt>NULL</tt>.</p></div>
1411
+ <h5 id="_tt_foreign_key_tt_2"><tt>:foreign_key</tt></h5>
1412
+ <div class="para"><p>By convention, Rails guesses that the column used to hold the foreign key on the other model is the name of this model with the suffix <tt>_id</tt> added. The <tt>:foreign_key</tt> option lets you set the name of the foreign key directly:</p></div>
1413
+ <div class="listingblock">
1414
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
1415
+ by Lorenzo Bettini
1416
+ http://www.lorenzobettini.it
1417
+ http://www.gnu.org/software/src-highlite -->
1418
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Supplier <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
1419
+ has_one <span style="color: #990000">:</span>account<span style="color: #990000">,</span> <span style="color: #990000">:</span>foreign_key <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"supp_id"</span>
1420
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1421
+ </tt></pre></div></div>
1422
+ <div class="admonitionblock">
1423
+ <table><tr>
1424
+ <td class="icon">
1425
+ <img src="./images/icons/tip.png" alt="Tip" />
1426
+ </td>
1427
+ <td class="content">In any case, Rails will not create foreign key columns for you. You need to explicitly define them as part of your migrations.</td>
1428
+ </tr></table>
1429
+ </div>
1430
+ <h5 id="_tt_include_tt_2"><tt>:include</tt></h5>
1431
+ <div class="para"><p>You can use the :include option to specify second-order associations that should be eager-loaded when this association is used. For example, consider these models:</p></div>
1432
+ <div class="listingblock">
1433
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
1434
+ by Lorenzo Bettini
1435
+ http://www.lorenzobettini.it
1436
+ http://www.gnu.org/software/src-highlite -->
1437
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Supplier <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
1438
+ has_one <span style="color: #990000">:</span>account
1439
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1440
+ <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Account <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
1441
+ belongs_to <span style="color: #990000">:</span>supplier
1442
+ belongs_to <span style="color: #990000">:</span>representative
1443
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1444
+ <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Representative <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
1445
+ has_many <span style="color: #990000">:</span>accounts
1446
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1447
+ </tt></pre></div></div>
1448
+ <div class="para"><p>If you frequently retrieve representatives directly from suppliers (<tt>@supplier.account.representative</tt>), then you can make your code somewhat more efficient by including representatives in the association from suppliers to accounts:</p></div>
1449
+ <div class="listingblock">
1450
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
1451
+ by Lorenzo Bettini
1452
+ http://www.lorenzobettini.it
1453
+ http://www.gnu.org/software/src-highlite -->
1454
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Supplier <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
1455
+ has_one <span style="color: #990000">:</span>account<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">=&gt;</span> <span style="color: #990000">:</span>representative
1456
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1457
+ <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Account <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
1458
+ belongs_to <span style="color: #990000">:</span>supplier
1459
+ belongs_to <span style="color: #990000">:</span>representative
1460
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1461
+ <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Representative <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
1462
+ has_many <span style="color: #990000">:</span>accounts
1463
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1464
+ </tt></pre></div></div>
1465
+ <h5 id="_tt_order_tt"><tt>:order</tt></h5>
1466
+ <div class="para"><p>The <tt>:order</tt> option dictates the order in which associated objects will be received (in the syntax used by a SQL <tt>ORDER BY</tt> clause). Because a <tt>has_one</tt> association will only retrieve a single associated object, this option should not be needed.</p></div>
1467
+ <h5 id="_tt_primary_key_tt"><tt>:primary_key</tt></h5>
1468
+ <div class="para"><p>By convention, Rails guesses that the column used to hold the primary key of this model is <tt>id</tt>. You can override this and explicitly specify the primary key with the <tt>:primary_key</tt> option.</p></div>
1469
+ <h5 id="_tt_readonly_tt_2"><tt>:readonly</tt></h5>
1470
+ <div class="para"><p>If you set the <tt>:readonly</tt> option to <tt>true</tt>, then the associated object will be read-only when retrieved via the association.</p></div>
1471
+ <h5 id="_tt_select_tt_2"><tt>:select</tt></h5>
1472
+ <div class="para"><p>The <tt>:select</tt> option lets you override the SQL <tt>SELECT</tt> clause that is used to retrieve data about the associated object. By default, Rails retrieves all columns.</p></div>
1473
+ <h5 id="_tt_source_tt"><tt>:source</tt></h5>
1474
+ <div class="para"><p>The <tt>:source</tt> option specifies the source association name for a <tt>has_one :through</tt> association.</p></div>
1475
+ <h5 id="_tt_source_type_tt"><tt>:source_type</tt></h5>
1476
+ <div class="para"><p>The <tt>:source_type</tt> option specifies the source association type for a <tt>has_one :through</tt> association that proceeds through a polymorphic association.</p></div>
1477
+ <h5 id="_tt_through_tt"><tt>:through</tt></h5>
1478
+ <div class="para"><p>The <tt>:through</tt> option specifies a join model through which to perform the query. <tt>has_one :through</tt> associations are discussed in detail later in this guide.</p></div>
1479
+ <h5 id="_tt_validate_tt_2"><tt>:validate</tt></h5>
1480
+ <div class="para"><p>If you set the <tt>:validate</tt> option to <tt>true</tt>, then associated objects will be validated whenever you save this object. By default, this is <tt>false</tt>: associated objects will not be validated when this object is saved.</p></div>
1481
+ <h4 id="_when_are_objects_saved_2">4.2.3. When are Objects Saved?</h4>
1482
+ <div class="para"><p>When you assign an object to a <tt>has_one</tt> association, that object is automatically saved (in order to update its foreign key). In addition, any object being replaced is also automatically saved, because its foreign key will change too.</p></div>
1483
+ <div class="para"><p>If either of these saves fails due to validation errors, then the assignment statement returns <tt>false</tt> and the assignment itself is cancelled.</p></div>
1484
+ <div class="para"><p>If the parent object (the one declaring the <tt>has_one</tt> association) is unsaved (that is, <tt>new_record?</tt> returns <tt>true</tt>) then the child objects are not saved.</p></div>
1485
+ <div class="para"><p>If you want to assign an object to a <tt>has_one</tt> association without saving the object, use the <tt>association.build</tt> method.</p></div>
1486
+ <h3 id="_the_has_many_association">4.3. The has_many Association</h3>
1487
+ <div class="para"><p>The <tt>has_many</tt> association creates a one-to-many relationship with another model. In database terms, this association says that the other class will have a foreign key that refers to instances of this class.</p></div>
1488
+ <h4 id="_methods_added">4.3.1. Methods Added</h4>
1489
+ <div class="para"><p>When you declare a <tt>has_many</tt> association, the declaring class automatically gains 13 methods related to the association:</p></div>
1490
+ <div class="ilist"><ul>
1491
+ <li>
1492
+ <p>
1493
+ <tt><em>collection</em>(force_reload = false)</tt>
1494
+ </p>
1495
+ </li>
1496
+ <li>
1497
+ <p>
1498
+ <tt><em>collection</em>&lt;&lt;(object, &#8230;)</tt>
1499
+ </p>
1500
+ </li>
1501
+ <li>
1502
+ <p>
1503
+ <tt><em>collection</em>.delete(object, &#8230;)</tt>
1504
+ </p>
1505
+ </li>
1506
+ <li>
1507
+ <p>
1508
+ <tt><em>collection</em>=objects</tt>
1509
+ </p>
1510
+ </li>
1511
+ <li>
1512
+ <p>
1513
+ <tt><em>collection_singular</em>_ids</tt>
1514
+ </p>
1515
+ </li>
1516
+ <li>
1517
+ <p>
1518
+ <tt><em>collection_singular</em>_ids=ids</tt>
1519
+ </p>
1520
+ </li>
1521
+ <li>
1522
+ <p>
1523
+ <tt><em>collection</em>.clear</tt>
1524
+ </p>
1525
+ </li>
1526
+ <li>
1527
+ <p>
1528
+ <tt><em>collection</em>.empty?</tt>
1529
+ </p>
1530
+ </li>
1531
+ <li>
1532
+ <p>
1533
+ <tt><em>collection</em>.size</tt>
1534
+ </p>
1535
+ </li>
1536
+ <li>
1537
+ <p>
1538
+ <tt><em>collection</em>.find(&#8230;)</tt>
1539
+ </p>
1540
+ </li>
1541
+ <li>
1542
+ <p>
1543
+ <tt><em>collection</em>.exist?(&#8230;)</tt>
1544
+ </p>
1545
+ </li>
1546
+ <li>
1547
+ <p>
1548
+ <tt><em>collection</em>.build(attributes = {}, &#8230;)</tt>
1549
+ </p>
1550
+ </li>
1551
+ <li>
1552
+ <p>
1553
+ <tt><em>collection</em>.create(attributes = {})</tt>
1554
+ </p>
1555
+ </li>
1556
+ </ul></div>
1557
+ <div class="para"><p>In all of these methods, <tt><em>collection</em></tt> is replaced with the symbol passed as the first argument to <tt>has_many</tt>, and <tt><em>collection_singular</em></tt> is replaced with the singularized version of that symbol.. For example, given the declaration:</p></div>
1558
+ <div class="listingblock">
1559
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
1560
+ by Lorenzo Bettini
1561
+ http://www.lorenzobettini.it
1562
+ http://www.gnu.org/software/src-highlite -->
1563
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
1564
+ has_many <span style="color: #990000">:</span>orders
1565
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1566
+ </tt></pre></div></div>
1567
+ <div class="para"><p>Each instance of the customer model will have these methods:</p></div>
1568
+ <div class="listingblock">
1569
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
1570
+ by Lorenzo Bettini
1571
+ http://www.lorenzobettini.it
1572
+ http://www.gnu.org/software/src-highlite -->
1573
+ <pre><tt>orders<span style="color: #990000">(</span>force_reload <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">)</span>
1574
+ orders<span style="color: #990000">&lt;&lt;(</span>object<span style="color: #990000">,</span> <span style="color: #990000">...)</span>
1575
+ orders<span style="color: #990000">.</span>delete<span style="color: #990000">(</span>object<span style="color: #990000">,</span> <span style="color: #990000">...)</span>
1576
+ orders<span style="color: #990000">=</span>objects
1577
+ order_ids
1578
+ order_ids<span style="color: #990000">=</span>ids
1579
+ orders<span style="color: #990000">.</span>clear
1580
+ orders<span style="color: #990000">.</span>empty?
1581
+ orders<span style="color: #990000">.</span>size
1582
+ orders<span style="color: #990000">.</span>find<span style="color: #990000">(...)</span>
1583
+ orders<span style="color: #990000">.</span>exist?<span style="color: #990000">(...)</span>
1584
+ orders<span style="color: #990000">.</span>build<span style="color: #990000">(</span>attributes <span style="color: #990000">=</span> <span style="color: #FF0000">{}</span><span style="color: #990000">,</span> <span style="color: #990000">...)</span>
1585
+ orders<span style="color: #990000">.</span>create<span style="color: #990000">(</span>attributes <span style="color: #990000">=</span> <span style="color: #FF0000">{}</span><span style="color: #990000">)</span>
1586
+ </tt></pre></div></div>
1587
+ <h5 id="_tt_em_collection_em_force_reload_false_tt"><tt><em>collection</em>(force_reload = false)</tt></h5>
1588
+ <div class="para"><p>The <tt><em>collection</em></tt> method returns an array of all of the associated objects. If there are no associated objects, it returns an empty array.</p></div>
1589
+ <div class="listingblock">
1590
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
1591
+ by Lorenzo Bettini
1592
+ http://www.lorenzobettini.it
1593
+ http://www.gnu.org/software/src-highlite -->
1594
+ <pre><tt><span style="color: #009900">@orders</span> <span style="color: #990000">=</span> <span style="color: #009900">@customer</span><span style="color: #990000">.</span>orders
1595
+ </tt></pre></div></div>
1596
+ <h5 id="_tt_em_collection_em_lt_lt_object_8230_tt"><tt><em>collection</em>&lt;&lt;(object, &#8230;)</tt></h5>
1597
+ <div class="para"><p>The <tt><em>collection</em>&lt;&lt;</tt> method adds one or more objects to the collection by setting their foreign keys to the primary key of the calling model.</p></div>
1598
+ <div class="listingblock">
1599
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
1600
+ by Lorenzo Bettini
1601
+ http://www.lorenzobettini.it
1602
+ http://www.gnu.org/software/src-highlite -->
1603
+ <pre><tt><span style="color: #009900">@customer</span><span style="color: #990000">.</span>orders <span style="color: #990000">&lt;&lt;</span> <span style="color: #009900">@order1</span>
1604
+ </tt></pre></div></div>
1605
+ <h5 id="_tt_em_collection_em_delete_object_8230_tt"><tt><em>collection</em>.delete(object, &#8230;)</tt></h5>
1606
+ <div class="para"><p>The <tt><em>collection</em>.delete</tt> method removes one or more objects from the collection by setting their foreign keys to <tt>NULL</tt>.</p></div>
1607
+ <div class="listingblock">
1608
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
1609
+ by Lorenzo Bettini
1610
+ http://www.lorenzobettini.it
1611
+ http://www.gnu.org/software/src-highlite -->
1612
+ <pre><tt><span style="color: #009900">@customer</span><span style="color: #990000">.</span>orders<span style="color: #990000">.</span>delete<span style="color: #990000">(</span><span style="color: #009900">@order1</span><span style="color: #990000">)</span>
1613
+ </tt></pre></div></div>
1614
+ <div class="admonitionblock">
1615
+ <table><tr>
1616
+ <td class="icon">
1617
+ <img src="./images/icons/warning.png" alt="Warning" />
1618
+ </td>
1619
+ <td class="content">Objects will be in addition destroyed if they're associated with <tt>:dependent &#8658; :destroy</tt>, and deleted if they're associated with <tt>:dependent &#8658; :delete_all</tt>.</td>
1620
+ </tr></table>
1621
+ </div>
1622
+ <h5 id="_tt_em_collection_em_objects_tt"><tt><em>collection</em>=objects</tt></h5>
1623
+ <div class="para"><p>The <tt><em>collection</em>=</tt> method makes the collection contain only the supplied objects, by adding and deleting as appropriate.</p></div>
1624
+ <h5 id="_tt_em_collection_singular_em_ids_tt"><tt><em>collection_singular</em>_ids</tt></h5>
1625
+ <div class="para"><p>The <tt><em>collection_singular</em>_ids</tt> method returns an array of the ids of the objects in the collection.</p></div>
1626
+ <div class="listingblock">
1627
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
1628
+ by Lorenzo Bettini
1629
+ http://www.lorenzobettini.it
1630
+ http://www.gnu.org/software/src-highlite -->
1631
+ <pre><tt><span style="color: #009900">@order_ids</span> <span style="color: #990000">=</span> <span style="color: #009900">@customer</span><span style="color: #990000">.</span>order_ids
1632
+ </tt></pre></div></div>
1633
+ <h5 id="_tt_em_collection_singular_em_ids_ids_tt"><tt><em>_collection_singular</em>_ids=ids</tt></h5>
1634
+ <div class="para"><p>The <tt><em>_collection_singular</em>_ids=</tt> method makes the collection contain only the objects identified by the supplied primary key values, by adding and deleting as appropriate.</p></div>
1635
+ <h5 id="_tt_em_collection_em_clear_tt"><tt><em>collection</em>.clear</tt></h5>
1636
+ <div class="para"><p>The <tt><em>collection</em>.clear</tt> method removes every object from the collection. This destroys the associated objects if they are associated with <tt>:dependent &#8658; :destroy</tt>, deletes them directly from the database if <tt>:dependent &#8658; :delete_all</tt>, and otherwise sets their foreign keys to <tt>NULL</tt>.</p></div>
1637
+ <h5 id="_tt_em_collection_em_empty_tt"><tt><em>collection</em>.empty?</tt></h5>
1638
+ <div class="para"><p>The <tt><em>collection</em>.empty?</tt> method returns <tt>true</tt> if the collection does not contain any associated objects.</p></div>
1639
+ <div class="listingblock">
1640
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
1641
+ by Lorenzo Bettini
1642
+ http://www.lorenzobettini.it
1643
+ http://www.gnu.org/software/src-highlite -->
1644
+ <pre><tt><span style="color: #FF0000">&lt;% if @customer.orders.empty? %&gt;</span>
1645
+ No Orders Found
1646
+ <span style="color: #FF0000">&lt;% end %&gt;</span>
1647
+ </tt></pre></div></div>
1648
+ <h5 id="_tt_em_collection_em_size_tt"><tt><em>collection</em>.size</tt></h5>
1649
+ <div class="para"><p>The <tt><em>collection</em>.size</tt> method returns the number of objects in the collection.</p></div>
1650
+ <div class="listingblock">
1651
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
1652
+ by Lorenzo Bettini
1653
+ http://www.lorenzobettini.it
1654
+ http://www.gnu.org/software/src-highlite -->
1655
+ <pre><tt><span style="color: #009900">@order_count</span> <span style="color: #990000">=</span> <span style="color: #009900">@customer</span><span style="color: #990000">.</span>orders<span style="color: #990000">.</span>size
1656
+ </tt></pre></div></div>
1657
+ <h5 id="_tt_em_collection_em_find_8230_tt"><tt><em>collection</em>.find(&#8230;)</tt></h5>
1658
+ <div class="para"><p>The <tt><em>collection</em>.find</tt> method finds objects within the collection. It uses the same syntax and options as <tt>ActiveRecord::Base.find</tt>.</p></div>
1659
+ <div class="listingblock">
1660
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
1661
+ by Lorenzo Bettini
1662
+ http://www.lorenzobettini.it
1663
+ http://www.gnu.org/software/src-highlite -->
1664
+ <pre><tt><span style="color: #009900">@open_orders</span> <span style="color: #990000">=</span> <span style="color: #009900">@customer</span><span style="color: #990000">.</span>orders<span style="color: #990000">.</span>find<span style="color: #990000">(:</span>all<span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"open = 1"</span><span style="color: #990000">)</span>
1665
+ </tt></pre></div></div>
1666
+ <h5 id="_tt_em_collection_em_exist_8230_tt"><tt><em>collection</em>.exist?(&#8230;)</tt></h5>
1667
+ <div class="para"><p>The <tt><em>collection</em>.exist?</tt> method checks whether an object meeting the supplied conditions exists in the collection. It uses the same syntax and options as <tt>ActiveRecord::Base.exists?</tt>.</p></div>
1668
+ <h5 id="_tt_em_collection_em_build_attributes_8230_tt"><tt><em>collection</em>.build(attributes = {}, &#8230;)</tt></h5>
1669
+ <div class="para"><p>The <tt><em>collection</em>.build</tt> method returns one or more new objects of the associated type. These objects will be instantiated from the passed attributes, and the link through their foreign key will be created, but the associated objects will <em>not</em> yet be saved.</p></div>
1670
+ <div class="listingblock">
1671
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
1672
+ by Lorenzo Bettini
1673
+ http://www.lorenzobettini.it
1674
+ http://www.gnu.org/software/src-highlite -->
1675
+ <pre><tt><span style="color: #009900">@order</span> <span style="color: #990000">=</span> <span style="color: #009900">@customer</span><span style="color: #990000">.</span>orders<span style="color: #990000">.</span>build<span style="color: #990000">(</span><span style="color: #FF0000">{</span><span style="color: #990000">:</span>order_date <span style="color: #990000">=&gt;</span> Time<span style="color: #990000">.</span>now<span style="color: #990000">,</span> <span style="color: #990000">:</span>order_number <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"A12345"</span><span style="color: #FF0000">}</span><span style="color: #990000">)</span>
1676
+ </tt></pre></div></div>
1677
+ <h5 id="_tt_em_collection_em_create_attributes_tt"><tt><em>collection</em>.create(attributes = {})</tt></h5>
1678
+ <div class="para"><p>The <tt><em>collection</em>.create</tt> method returns a new object of the associated type. This object will be instantiated from the passed attributes, the link through its foreign key will be created, and the associated object <em>will</em> be saved (assuming that it passes any validations).</p></div>
1679
+ <div class="listingblock">
1680
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
1681
+ by Lorenzo Bettini
1682
+ http://www.lorenzobettini.it
1683
+ http://www.gnu.org/software/src-highlite -->
1684
+ <pre><tt><span style="color: #009900">@order</span> <span style="color: #990000">=</span> <span style="color: #009900">@customer</span><span style="color: #990000">.</span>orders<span style="color: #990000">.</span>create<span style="color: #990000">(</span><span style="color: #FF0000">{</span><span style="color: #990000">:</span>order_date <span style="color: #990000">=&gt;</span> Time<span style="color: #990000">.</span>now<span style="color: #990000">,</span> <span style="color: #990000">:</span>order_number <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"A12345"</span><span style="color: #FF0000">}</span><span style="color: #990000">)</span>
1685
+ </tt></pre></div></div>
1686
+ <h4 id="_options_for_has_many">4.3.2. Options for has_many</h4>
1687
+ <div class="para"><p>In many situations, you can use the default behavior for <tt>has_many</tt> without any customization. But you can alter that behavior in a number of ways. This section covers the options that you can pass when you create a <tt>has_many</tt> association. For example, an association with several options might look like this:</p></div>
1688
+ <div class="listingblock">
1689
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
1690
+ by Lorenzo Bettini
1691
+ http://www.lorenzobettini.it
1692
+ http://www.gnu.org/software/src-highlite -->
1693
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
1694
+ has_many <span style="color: #990000">:</span>orders<span style="color: #990000">,</span> <span style="color: #990000">:</span>dependent <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>delete_all<span style="color: #990000">,</span> <span style="color: #990000">:</span>validate <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">false</span></span>
1695
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1696
+ </tt></pre></div></div>
1697
+ <div class="para"><p>The <tt>has_many</tt> association supports these options:</p></div>
1698
+ <div class="ilist"><ul>
1699
+ <li>
1700
+ <p>
1701
+ <tt>:as</tt>
1702
+ </p>
1703
+ </li>
1704
+ <li>
1705
+ <p>
1706
+ <tt>:class_name</tt>
1707
+ </p>
1708
+ </li>
1709
+ <li>
1710
+ <p>
1711
+ <tt>:conditions</tt>
1712
+ </p>
1713
+ </li>
1714
+ <li>
1715
+ <p>
1716
+ <tt>:counter_sql</tt>
1717
+ </p>
1718
+ </li>
1719
+ <li>
1720
+ <p>
1721
+ <tt>:dependent</tt>
1722
+ </p>
1723
+ </li>
1724
+ <li>
1725
+ <p>
1726
+ <tt>:extend</tt>
1727
+ </p>
1728
+ </li>
1729
+ <li>
1730
+ <p>
1731
+ <tt>:finder_sql</tt>
1732
+ </p>
1733
+ </li>
1734
+ <li>
1735
+ <p>
1736
+ <tt>:foreign_key</tt>
1737
+ </p>
1738
+ </li>
1739
+ <li>
1740
+ <p>
1741
+ <tt>:group</tt>
1742
+ </p>
1743
+ </li>
1744
+ <li>
1745
+ <p>
1746
+ <tt>:include</tt>
1747
+ </p>
1748
+ </li>
1749
+ <li>
1750
+ <p>
1751
+ <tt>:limit</tt>
1752
+ </p>
1753
+ </li>
1754
+ <li>
1755
+ <p>
1756
+ <tt>:offset</tt>
1757
+ </p>
1758
+ </li>
1759
+ <li>
1760
+ <p>
1761
+ <tt>:order</tt>
1762
+ </p>
1763
+ </li>
1764
+ <li>
1765
+ <p>
1766
+ <tt>:primary_key</tt>
1767
+ </p>
1768
+ </li>
1769
+ <li>
1770
+ <p>
1771
+ <tt>:readonly</tt>
1772
+ </p>
1773
+ </li>
1774
+ <li>
1775
+ <p>
1776
+ <tt>:select</tt>
1777
+ </p>
1778
+ </li>
1779
+ <li>
1780
+ <p>
1781
+ <tt>:source</tt>
1782
+ </p>
1783
+ </li>
1784
+ <li>
1785
+ <p>
1786
+ <tt>:source_type</tt>
1787
+ </p>
1788
+ </li>
1789
+ <li>
1790
+ <p>
1791
+ <tt>:through</tt>
1792
+ </p>
1793
+ </li>
1794
+ <li>
1795
+ <p>
1796
+ <tt>:uniq</tt>
1797
+ </p>
1798
+ </li>
1799
+ <li>
1800
+ <p>
1801
+ <tt>:validate</tt>
1802
+ </p>
1803
+ </li>
1804
+ </ul></div>
1805
+ <h5 id="_tt_as_tt_2"><tt>:as</tt></h5>
1806
+ <div class="para"><p>Setting the <tt>:as</tt> option indicates that this is a polymorphic association, as discussed earlier in this guide.</p></div>
1807
+ <h5 id="_tt_class_name_tt_3"><tt>:class_name</tt></h5>
1808
+ <div class="para"><p>If the name of the other model cannot be derived from the association name, you can use the <tt>:class_name</tt> option to supply the model name. For example, if a customer has many orders, but the actual name of the model containing orders is <tt>Transaction</tt>, you'd set things up this way:</p></div>
1809
+ <div class="listingblock">
1810
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
1811
+ by Lorenzo Bettini
1812
+ http://www.lorenzobettini.it
1813
+ http://www.gnu.org/software/src-highlite -->
1814
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
1815
+ has_many <span style="color: #990000">:</span>orders<span style="color: #990000">,</span> <span style="color: #990000">:</span>class_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Transaction"</span>
1816
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1817
+ </tt></pre></div></div>
1818
+ <h5 id="_tt_conditions_tt_3"><tt>:conditions</tt></h5>
1819
+ <div class="para"><p>The <tt>:conditions</tt> option lets you specify the conditions that the associated object must meet (in the syntax used by a SQL <tt>WHERE</tt> clause).</p></div>
1820
+ <div class="listingblock">
1821
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
1822
+ by Lorenzo Bettini
1823
+ http://www.lorenzobettini.it
1824
+ http://www.gnu.org/software/src-highlite -->
1825
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
1826
+ has_many <span style="color: #990000">:</span>confirmed_orders<span style="color: #990000">,</span> <span style="color: #990000">:</span>class_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Order"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"confirmed = 1"</span>
1827
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1828
+ </tt></pre></div></div>
1829
+ <div class="para"><p>You can also set conditions via a hash:</p></div>
1830
+ <div class="listingblock">
1831
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
1832
+ by Lorenzo Bettini
1833
+ http://www.lorenzobettini.it
1834
+ http://www.gnu.org/software/src-highlite -->
1835
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
1836
+ has_many <span style="color: #990000">:</span>confirmed_orders<span style="color: #990000">,</span> <span style="color: #990000">:</span>class_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Order"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>confirmed <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span> <span style="color: #FF0000">}</span>
1837
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1838
+ </tt></pre></div></div>
1839
+ <div class="para"><p>If you use a hash-style <tt>:conditions</tt> option, then record creation via this association will be automatically scoped using the hash. In this case, using <tt>@customer.confirmed_orders.create</tt> or <tt>@customer.confirmed_orders.build</tt> will create orders where the confirmed column has the value <tt>true</tt>.</p></div>
1840
+ <h5 id="_tt_counter_sql_tt"><tt>:counter_sql</tt></h5>
1841
+ <div class="para"><p>Normally Rails automatically generates the proper SQL to count the association members. With the <tt>:counter_sql</tt> option, you can specify a complete SQL statement to count them yourself.</p></div>
1842
+ <div class="admonitionblock">
1843
+ <table><tr>
1844
+ <td class="icon">
1845
+ <img src="./images/icons/note.png" alt="Note" />
1846
+ </td>
1847
+ <td class="content">If you specify <tt>:finder_sql</tt> but not <tt>:counter_sql</tt>, then the counter SQL will be generated by substituting <tt>SELECT COUNT(*) FROM</tt> for the <tt>SELECT &#8230; FROM</tt> clause of your <tt>:finder_sql</tt> statement.</td>
1848
+ </tr></table>
1849
+ </div>
1850
+ <h5 id="_tt_dependent_tt_3"><tt>:dependent</tt></h5>
1851
+ <div class="para"><p>If you set the <tt>:dependent</tt> option to <tt>:destroy</tt>, then deleting this object will call the destroy method on the associated objects to delete those objects. If you set the <tt>:dependent</tt> option to <tt>:delete_all</tt>, then deleting this object will delete the associated objects <em>without</em> calling their <tt>destroy</tt> method. If you set the <tt>:dependent</tt> option to <tt>:nullify</tt>, then deleting this object will set the foreign key in the associated objects to <tt>NULL</tt>.</p></div>
1852
+ <div class="admonitionblock">
1853
+ <table><tr>
1854
+ <td class="icon">
1855
+ <img src="./images/icons/note.png" alt="Note" />
1856
+ </td>
1857
+ <td class="content">This option is ignored when you use the <tt>:through</tt> option on the association.</td>
1858
+ </tr></table>
1859
+ </div>
1860
+ <h5 id="_tt_extend_tt"><tt>:extend</tt></h5>
1861
+ <div class="para"><p>The <tt>:extend</tt> option specifies a named module to extend the association proxy. Association extensions are discussed in detail later in this guide.</p></div>
1862
+ <h5 id="_tt_finder_sql_tt"><tt>:finder_sql</tt></h5>
1863
+ <div class="para"><p>Normally Rails automatically generates the proper SQL to fetch the association members. With the <tt>:finder_sql</tt> option, you can specify a complete SQL statement to fetch them yourself. If fetching objects requires complex multi-table SQL, this may be necessary.</p></div>
1864
+ <h5 id="_tt_foreign_key_tt_3"><tt>:foreign_key</tt></h5>
1865
+ <div class="para"><p>By convention, Rails guesses that the column used to hold the foreign key on the other model is the name of this model with the suffix <tt>_id</tt> added. The <tt>:foreign_key</tt> option lets you set the name of the foreign key directly:</p></div>
1866
+ <div class="listingblock">
1867
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
1868
+ by Lorenzo Bettini
1869
+ http://www.lorenzobettini.it
1870
+ http://www.gnu.org/software/src-highlite -->
1871
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
1872
+ has_many <span style="color: #990000">:</span>orders<span style="color: #990000">,</span> <span style="color: #990000">:</span>foreign_key <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"cust_id"</span>
1873
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1874
+ </tt></pre></div></div>
1875
+ <div class="admonitionblock">
1876
+ <table><tr>
1877
+ <td class="icon">
1878
+ <img src="./images/icons/tip.png" alt="Tip" />
1879
+ </td>
1880
+ <td class="content">In any case, Rails will not create foreign key columns for you. You need to explicitly define them as part of your migrations.</td>
1881
+ </tr></table>
1882
+ </div>
1883
+ <h5 id="_tt_group_tt"><tt>:group</tt></h5>
1884
+ <div class="para"><p>The <tt>:group</tt> option supplies an attribute name to group the result set by, using a <tt>GROUP BY</tt> clause in the finder SQL.</p></div>
1885
+ <div class="listingblock">
1886
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
1887
+ by Lorenzo Bettini
1888
+ http://www.lorenzobettini.it
1889
+ http://www.gnu.org/software/src-highlite -->
1890
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
1891
+ has_many <span style="color: #990000">:</span>line_items<span style="color: #990000">,</span> <span style="color: #990000">:</span>through <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>orders<span style="color: #990000">,</span> <span style="color: #990000">:</span>group <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"orders.id"</span>
1892
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1893
+ </tt></pre></div></div>
1894
+ <h5 id="_tt_include_tt_3"><tt>:include</tt></h5>
1895
+ <div class="para"><p>You can use the :include option to specify second-order associations that should be eager-loaded when this association is used. For example, consider these models:</p></div>
1896
+ <div class="listingblock">
1897
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
1898
+ by Lorenzo Bettini
1899
+ http://www.lorenzobettini.it
1900
+ http://www.gnu.org/software/src-highlite -->
1901
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
1902
+ has_many <span style="color: #990000">:</span>orders
1903
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1904
+ <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Order <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
1905
+ belongs_to <span style="color: #990000">:</span>customer
1906
+ has_many <span style="color: #990000">:</span>line_items
1907
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1908
+ <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> LineItem <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
1909
+ belongs_to <span style="color: #990000">:</span>order
1910
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1911
+ </tt></pre></div></div>
1912
+ <div class="para"><p>If you frequently retrieve line items directly from customers (<tt>@customer.orders.line_items</tt>), then you can make your code somewhat more efficient by including line items in the association from customers to orders:</p></div>
1913
+ <div class="listingblock">
1914
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
1915
+ by Lorenzo Bettini
1916
+ http://www.lorenzobettini.it
1917
+ http://www.gnu.org/software/src-highlite -->
1918
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
1919
+ has_many <span style="color: #990000">:</span>orders<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">=&gt;</span> <span style="color: #990000">:</span>line_items
1920
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1921
+ <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Order <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
1922
+ belongs_to <span style="color: #990000">:</span>customer
1923
+ has_many <span style="color: #990000">:</span>line_items
1924
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1925
+ <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> LineItem <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
1926
+ belongs_to <span style="color: #990000">:</span>order
1927
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1928
+ </tt></pre></div></div>
1929
+ <h5 id="_tt_limit_tt"><tt>:limit</tt></h5>
1930
+ <div class="para"><p>The <tt>:limit</tt> option lets you restrict the total number of objects that will be fetched through an association.</p></div>
1931
+ <div class="listingblock">
1932
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
1933
+ by Lorenzo Bettini
1934
+ http://www.lorenzobettini.it
1935
+ http://www.gnu.org/software/src-highlite -->
1936
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
1937
+ has_many <span style="color: #990000">:</span>recent_orders<span style="color: #990000">,</span> <span style="color: #990000">:</span>class_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Order"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>order <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"order_date DESC"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>limit <span style="color: #990000">=&gt;</span> <span style="color: #993399">100</span>
1938
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1939
+ </tt></pre></div></div>
1940
+ <h5 id="_tt_offset_tt"><tt>:offset</tt></h5>
1941
+ <div class="para"><p>The <tt>:offset</tt> option lets you specify the starting offset for fetching objects via an association. For example, if you set <tt>:offset &#8658; 11</tt>, it will skip the first 10 records.</p></div>
1942
+ <h5 id="_tt_order_tt_2"><tt>:order</tt></h5>
1943
+ <div class="para"><p>The <tt>:order</tt> option dictates the order in which associated objects will be received (in the syntax used by a SQL <tt>ORDER BY</tt> clause).</p></div>
1944
+ <div class="listingblock">
1945
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
1946
+ by Lorenzo Bettini
1947
+ http://www.lorenzobettini.it
1948
+ http://www.gnu.org/software/src-highlite -->
1949
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
1950
+ has_many <span style="color: #990000">:</span>orders<span style="color: #990000">,</span> <span style="color: #990000">:</span>order <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"date_confirmed DESC"</span>
1951
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
1952
+ </tt></pre></div></div>
1953
+ <h5 id="_tt_primary_key_tt_2"><tt>:primary_key</tt></h5>
1954
+ <div class="para"><p>By convention, Rails guesses that the column used to hold the primary key of this model is <tt>id</tt>. You can override this and explicitly specify the primary key with the <tt>:primary_key</tt> option.</p></div>
1955
+ <h5 id="_tt_readonly_tt_3"><tt>:readonly</tt></h5>
1956
+ <div class="para"><p>If you set the <tt>:readonly</tt> option to <tt>true</tt>, then the associated objects will be read-only when retrieved via the association.</p></div>
1957
+ <h5 id="_tt_select_tt_3"><tt>:select</tt></h5>
1958
+ <div class="para"><p>The <tt>:select</tt> option lets you override the SQL <tt>SELECT</tt> clause that is used to retrieve data about the associated objects. By default, Rails retrieves all columns.</p></div>
1959
+ <div class="admonitionblock">
1960
+ <table><tr>
1961
+ <td class="icon">
1962
+ <img src="./images/icons/warning.png" alt="Warning" />
1963
+ </td>
1964
+ <td class="content">If you specify your own <tt>:select</tt>, be sure to include the primary key and foreign key columns of the associated model. If you do not, Rails will throw an error.</td>
1965
+ </tr></table>
1966
+ </div>
1967
+ <h5 id="_tt_source_tt_2"><tt>:source</tt></h5>
1968
+ <div class="para"><p>The <tt>:source</tt> option specifies the source association name for a <tt>has_many :through</tt> association. You only need to use this option if the name of the source association cannot be automatically inferred from the association name.</p></div>
1969
+ <h5 id="_tt_source_type_tt_2"><tt>:source_type</tt></h5>
1970
+ <div class="para"><p>The <tt>:source_type</tt> option specifies the source association type for a <tt>has_many :through</tt> association that proceeds through a polymorphic association.</p></div>
1971
+ <h5 id="_tt_through_tt_2"><tt>:through</tt></h5>
1972
+ <div class="para"><p>The <tt>:through</tt> option specifies a join model through which to perform the query. <tt>has_many :through</tt> associations provide a way to implement many-to-many relationships, as discussed earlier in this guide.</p></div>
1973
+ <h5 id="_tt_uniq_tt"><tt>:uniq</tt></h5>
1974
+ <div class="para"><p>Specify the <tt>:uniq &#8658; true</tt> option to remove duplicates from the collection. This is most useful in conjunction with the <tt>:through</tt> option.</p></div>
1975
+ <h5 id="_tt_validate_tt_3"><tt>:validate</tt></h5>
1976
+ <div class="para"><p>If you set the <tt>:validate</tt> option to <tt>false</tt>, then associated objects will not be validated whenever you save this object. By default, this is <tt>true</tt>: associated objects will be validated when this object is saved.</p></div>
1977
+ <h4 id="_when_are_objects_saved_3">4.3.3. When are Objects Saved?</h4>
1978
+ <div class="para"><p>When you assign an object to a <tt>has_many</tt> association, that object is automatically saved (in order to update its foreign key). If you assign multiple objects in one statement, then they are all saved.</p></div>
1979
+ <div class="para"><p>If any of these saves fails due to validation errors, then the assignment statement returns <tt>false</tt> and the assignment itself is cancelled.</p></div>
1980
+ <div class="para"><p>If the parent object (the one declaring the <tt>has_many</tt> association) is unsaved (that is, <tt>new_record?</tt> returns <tt>true</tt>) then the child objects are not saved when they are added. All unsaved members of the association will automatically be saved when the parent is saved.</p></div>
1981
+ <div class="para"><p>If you want to assign an object to a <tt>has_many</tt> association without saving the object, use the <tt><em>collection</em>.build</tt> method.</p></div>
1982
+ <h3 id="_the_tt_has_and_belongs_to_many_tt_association_2">4.4. The <tt>has_and_belongs_to_many</tt> Association</h3>
1983
+ <div class="para"><p>The <tt>has_and_belongs_to_many</tt> association creates a many-to-many relationship with another model. In database terms, this associates two classes via an intermediate join table that includes foreign keys referring to each of the classes.</p></div>
1984
+ <h4 id="_methods_added_2">4.4.1. Methods Added</h4>
1985
+ <div class="para"><p>When you declare a <tt>has_and_belongs_to_many</tt> association, the declaring class automatically gains 13 methods related to the association:</p></div>
1986
+ <div class="ilist"><ul>
1987
+ <li>
1988
+ <p>
1989
+ <tt><em>collection</em>(force_reload = false)</tt>
1990
+ </p>
1991
+ </li>
1992
+ <li>
1993
+ <p>
1994
+ <tt><em>collection</em>&lt;&lt;(object, &#8230;)</tt>
1995
+ </p>
1996
+ </li>
1997
+ <li>
1998
+ <p>
1999
+ <tt><em>collection</em>.delete(object, &#8230;)</tt>
2000
+ </p>
2001
+ </li>
2002
+ <li>
2003
+ <p>
2004
+ <tt><em>collection</em>=objects</tt>
2005
+ </p>
2006
+ </li>
2007
+ <li>
2008
+ <p>
2009
+ <tt><em>collection_singular</em>_ids</tt>
2010
+ </p>
2011
+ </li>
2012
+ <li>
2013
+ <p>
2014
+ <tt><em>collection_singular</em>_ids=ids</tt>
2015
+ </p>
2016
+ </li>
2017
+ <li>
2018
+ <p>
2019
+ <tt><em>collection</em>.clear</tt>
2020
+ </p>
2021
+ </li>
2022
+ <li>
2023
+ <p>
2024
+ <tt><em>collection</em>.empty?</tt>
2025
+ </p>
2026
+ </li>
2027
+ <li>
2028
+ <p>
2029
+ <tt><em>collection</em>.size</tt>
2030
+ </p>
2031
+ </li>
2032
+ <li>
2033
+ <p>
2034
+ <tt><em>collection</em>.find(&#8230;)</tt>
2035
+ </p>
2036
+ </li>
2037
+ <li>
2038
+ <p>
2039
+ <tt><em>collection</em>.exist?(&#8230;)</tt>
2040
+ </p>
2041
+ </li>
2042
+ <li>
2043
+ <p>
2044
+ <tt><em>collection</em>.build(attributes = {})</tt>
2045
+ </p>
2046
+ </li>
2047
+ <li>
2048
+ <p>
2049
+ <tt><em>collection</em>.create(attributes = {})</tt>
2050
+ </p>
2051
+ </li>
2052
+ </ul></div>
2053
+ <div class="para"><p>In all of these methods, <tt><em>collection</em></tt> is replaced with the symbol passed as the first argument to <tt>has_many</tt>, and <tt><em>collection</em>_singular</tt> is replaced with the singularized version of that symbol.. For example, given the declaration:</p></div>
2054
+ <div class="listingblock">
2055
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
2056
+ by Lorenzo Bettini
2057
+ http://www.lorenzobettini.it
2058
+ http://www.gnu.org/software/src-highlite -->
2059
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Part <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
2060
+ has_and_belongs_to_many <span style="color: #990000">:</span>assemblies
2061
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
2062
+ </tt></pre></div></div>
2063
+ <div class="para"><p>Each instance of the part model will have these methods:</p></div>
2064
+ <div class="listingblock">
2065
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
2066
+ by Lorenzo Bettini
2067
+ http://www.lorenzobettini.it
2068
+ http://www.gnu.org/software/src-highlite -->
2069
+ <pre><tt>assemblies<span style="color: #990000">(</span>force_reload <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">)</span>
2070
+ assemblies<span style="color: #990000">&lt;&lt;(</span>object<span style="color: #990000">,</span> <span style="color: #990000">...)</span>
2071
+ assemblies<span style="color: #990000">.</span>delete<span style="color: #990000">(</span>object<span style="color: #990000">,</span> <span style="color: #990000">...)</span>
2072
+ assemblies<span style="color: #990000">=</span>objects
2073
+ assembly_ids
2074
+ assembly_ids<span style="color: #990000">=</span>ids
2075
+ assemblies<span style="color: #990000">.</span>clear
2076
+ assemblies<span style="color: #990000">.</span>empty?
2077
+ assemblies<span style="color: #990000">.</span>size
2078
+ assemblies<span style="color: #990000">.</span>find<span style="color: #990000">(...)</span>
2079
+ assemblies<span style="color: #990000">.</span>exist?<span style="color: #990000">(...)</span>
2080
+ assemblies<span style="color: #990000">.</span>build<span style="color: #990000">(</span>attributes <span style="color: #990000">=</span> <span style="color: #FF0000">{}</span><span style="color: #990000">,</span> <span style="color: #990000">...)</span>
2081
+ assemblies<span style="color: #990000">.</span>create<span style="color: #990000">(</span>attributes <span style="color: #990000">=</span> <span style="color: #FF0000">{}</span><span style="color: #990000">)</span>
2082
+ </tt></pre></div></div>
2083
+ <h5 id="_additional_column_methods">Additional Column Methods</h5>
2084
+ <div class="para"><p>If the join table for a <tt>has_and_belongs_to_many</tt> association has additional columns beyond the two foreign keys, these columns will be added as attributes to records retrieved via that association. Records returned with additional attributes will always be read-only, because Rails cannot save changes to those attributes.</p></div>
2085
+ <div class="admonitionblock">
2086
+ <table><tr>
2087
+ <td class="icon">
2088
+ <img src="./images/icons/warning.png" alt="Warning" />
2089
+ </td>
2090
+ <td class="content">The use of extra attributes on the join table in a <tt>has_and_belongs_to_many</tt> association is deprecated. If you require this sort of complex behavior on the table that joins two models in a many-to-many relationship, you should use a <tt>has_many :through</tt> association instead of <tt>has_and_belongs_to_many</tt>.</td>
2091
+ </tr></table>
2092
+ </div>
2093
+ <h5 id="_tt_em_collection_em_force_reload_false_tt_2"><tt><em>collection</em>(force_reload = false)</tt></h5>
2094
+ <div class="para"><p>The <tt><em>collection</em></tt> method returns an array of all of the associated objects. If there are no associated objects, it returns an empty array.</p></div>
2095
+ <div class="listingblock">
2096
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
2097
+ by Lorenzo Bettini
2098
+ http://www.lorenzobettini.it
2099
+ http://www.gnu.org/software/src-highlite -->
2100
+ <pre><tt><span style="color: #009900">@assemblies</span> <span style="color: #990000">=</span> <span style="color: #009900">@part</span><span style="color: #990000">.</span>assemblies
2101
+ </tt></pre></div></div>
2102
+ <h5 id="_tt_em_collection_em_lt_lt_object_8230_tt_2"><tt><em>collection</em>&lt;&lt;(object, &#8230;)</tt></h5>
2103
+ <div class="para"><p>The <tt><em>collection</em>&lt;&lt;</tt> method adds one or more objects to the collection by creating records in the join table.</p></div>
2104
+ <div class="listingblock">
2105
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
2106
+ by Lorenzo Bettini
2107
+ http://www.lorenzobettini.it
2108
+ http://www.gnu.org/software/src-highlite -->
2109
+ <pre><tt><span style="color: #009900">@part</span><span style="color: #990000">.</span>assemblies <span style="color: #990000">&lt;&lt;</span> <span style="color: #009900">@assembly1</span>
2110
+ </tt></pre></div></div>
2111
+ <div class="admonitionblock">
2112
+ <table><tr>
2113
+ <td class="icon">
2114
+ <img src="./images/icons/note.png" alt="Note" />
2115
+ </td>
2116
+ <td class="content">This method is aliased as <tt><em>collection</em>.concat</tt> and <tt><em>collection</em>.push</tt>.</td>
2117
+ </tr></table>
2118
+ </div>
2119
+ <h5 id="_tt_em_collection_em_delete_object_8230_tt_2"><tt><em>collection</em>.delete(object, &#8230;)</tt></h5>
2120
+ <div class="para"><p>The <tt><em>collection</em>.delete</tt> method removes one or more objects from the collection by deleting records in the join table. This does not destroy the objects.</p></div>
2121
+ <div class="listingblock">
2122
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
2123
+ by Lorenzo Bettini
2124
+ http://www.lorenzobettini.it
2125
+ http://www.gnu.org/software/src-highlite -->
2126
+ <pre><tt><span style="color: #009900">@part</span><span style="color: #990000">.</span>assemblies<span style="color: #990000">.</span>delete<span style="color: #990000">(</span><span style="color: #009900">@assembly1</span><span style="color: #990000">)</span>
2127
+ </tt></pre></div></div>
2128
+ <h5 id="_tt_em_collection_em_objects_tt_2"><tt><em>collection</em>=objects</tt></h5>
2129
+ <div class="para"><p>The <tt><em>collection</em>=</tt> method makes the collection contain only the supplied objects, by adding and deleting as appropriate.</p></div>
2130
+ <h5 id="_tt_em_collection_singular_em_ids_tt_2"><tt><em>collection_singular</em>_ids</tt></h5>
2131
+ <div class="para"><p># Returns an array of the associated objects' ids</p></div>
2132
+ <div class="para"><p>The <tt><em>collection_singular</em>_ids</tt> method returns an array of the ids of the objects in the collection.</p></div>
2133
+ <div class="listingblock">
2134
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
2135
+ by Lorenzo Bettini
2136
+ http://www.lorenzobettini.it
2137
+ http://www.gnu.org/software/src-highlite -->
2138
+ <pre><tt><span style="color: #009900">@assembly_ids</span> <span style="color: #990000">=</span> <span style="color: #009900">@part</span><span style="color: #990000">.</span>assembly_ids
2139
+ </tt></pre></div></div>
2140
+ <h5 id="_tt_em_collection_singular_em_ids_ids_tt_2"><tt><em>collection_singular</em>_ids=ids</tt></h5>
2141
+ <div class="para"><p>The <tt><em>collection_singular</em>_ids=</tt> method makes the collection contain only the objects identified by the supplied primary key values, by adding and deleting as appropriate.</p></div>
2142
+ <h5 id="_tt_em_collection_em_clear_tt_2"><tt><em>collection</em>.clear</tt></h5>
2143
+ <div class="para"><p>The <tt><em>collection</em>.clear</tt> method removes every object from the collection by deleting the rows from the joining tableassociation. This does not destroy the associated objects.</p></div>
2144
+ <h5 id="_tt_em_collection_em_empty_tt_2"><tt><em>collection</em>.empty?</tt></h5>
2145
+ <div class="para"><p>The <tt><em>collection</em>.empty?</tt> method returns <tt>true</tt> if the collection does not contain any associated objects.</p></div>
2146
+ <div class="listingblock">
2147
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
2148
+ by Lorenzo Bettini
2149
+ http://www.lorenzobettini.it
2150
+ http://www.gnu.org/software/src-highlite -->
2151
+ <pre><tt><span style="color: #FF0000">&lt;% if @part.assemblies.empty? %&gt;</span>
2152
+ This part is <span style="font-weight: bold"><span style="color: #0000FF">not</span></span> used <span style="font-weight: bold"><span style="color: #0000FF">in</span></span> any assemblies
2153
+ <span style="color: #FF0000">&lt;% end %&gt;</span>
2154
+ </tt></pre></div></div>
2155
+ <h5 id="_tt_em_collection_em_size_tt_2"><tt><em>collection</em>.size</tt></h5>
2156
+ <div class="para"><p>The <tt><em>collection</em>.size</tt> method returns the number of objects in the collection.</p></div>
2157
+ <div class="listingblock">
2158
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
2159
+ by Lorenzo Bettini
2160
+ http://www.lorenzobettini.it
2161
+ http://www.gnu.org/software/src-highlite -->
2162
+ <pre><tt><span style="color: #009900">@assembly_count</span> <span style="color: #990000">=</span> <span style="color: #009900">@part</span><span style="color: #990000">.</span>assemblies<span style="color: #990000">.</span>size
2163
+ </tt></pre></div></div>
2164
+ <h5 id="_tt_em_collection_em_find_8230_tt_2"><tt><em>collection</em>.find(&#8230;)</tt></h5>
2165
+ <div class="para"><p>The <tt><em>collection</em>.find</tt> method finds objects within the collection. It uses the same syntax and options as <tt>ActiveRecord::Base.find</tt>. It also adds the additional condition that the object must be in the collection.</p></div>
2166
+ <div class="listingblock">
2167
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
2168
+ by Lorenzo Bettini
2169
+ http://www.lorenzobettini.it
2170
+ http://www.gnu.org/software/src-highlite -->
2171
+ <pre><tt><span style="color: #009900">@new_assemblies</span> <span style="color: #990000">=</span> <span style="color: #009900">@part</span><span style="color: #990000">.</span>assemblies<span style="color: #990000">.</span>find<span style="color: #990000">(:</span>all<span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #990000">[</span><span style="color: #FF0000">"created_at &gt; ?"</span><span style="color: #990000">,</span> <span style="color: #993399">2</span><span style="color: #990000">.</span>days<span style="color: #990000">.</span>ago<span style="color: #990000">])</span>
2172
+ </tt></pre></div></div>
2173
+ <h5 id="_tt_em_collection_em_exist_8230_tt_2"><tt><em>collection</em>.exist?(&#8230;)</tt></h5>
2174
+ <div class="para"><p>The <tt><em>collection</em>.exist?</tt> method checks whether an object meeting the supplied conditions exists in the collection. It uses the same syntax and options as <tt>ActiveRecord::Base.exists?</tt>.</p></div>
2175
+ <h5 id="_tt_em_collection_em_build_attributes_tt"><tt><em>collection</em>.build(attributes = {})</tt></h5>
2176
+ <div class="para"><p>The <tt><em>collection</em>.build</tt> method returns a new object of the associated type. This object will be instantiated from the passed attributes, and the link through the join table will be created, but the associated object will <em>not</em> yet be saved.</p></div>
2177
+ <div class="listingblock">
2178
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
2179
+ by Lorenzo Bettini
2180
+ http://www.lorenzobettini.it
2181
+ http://www.gnu.org/software/src-highlite -->
2182
+ <pre><tt><span style="color: #009900">@assembly</span> <span style="color: #990000">=</span> <span style="color: #009900">@part</span><span style="color: #990000">.</span>assemblies<span style="color: #990000">.</span>build<span style="color: #990000">(</span><span style="color: #FF0000">{</span><span style="color: #990000">:</span>assembly_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Transmission housing"</span><span style="color: #FF0000">}</span><span style="color: #990000">)</span>
2183
+ </tt></pre></div></div>
2184
+ <h5 id="_tt_em_collection_em_create_attributes_tt_2"><tt><em>collection</em>.create(attributes = {})</tt></h5>
2185
+ <div class="para"><p>The <tt><em>collection</em>.create</tt> method returns a new object of the associated type. This objects will be instantiated from the passed attributes, the link through the join table will be created, and the associated object <em>will</em> be saved (assuming that it passes any validations).</p></div>
2186
+ <div class="listingblock">
2187
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
2188
+ by Lorenzo Bettini
2189
+ http://www.lorenzobettini.it
2190
+ http://www.gnu.org/software/src-highlite -->
2191
+ <pre><tt><span style="color: #009900">@assembly</span> <span style="color: #990000">=</span> <span style="color: #009900">@part</span><span style="color: #990000">.</span>assemblies<span style="color: #990000">.</span>create<span style="color: #990000">(</span><span style="color: #FF0000">{</span><span style="color: #990000">:</span>assembly_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Transmission housing"</span><span style="color: #FF0000">}</span><span style="color: #990000">)</span>
2192
+ </tt></pre></div></div>
2193
+ <h4 id="_options_for_has_and_belongs_to_many">4.4.2. Options for has_and_belongs_to_many</h4>
2194
+ <div class="para"><p>In many situations, you can use the default behavior for <tt>has_and_belongs_to_many</tt> without any customization. But you can alter that behavior in a number of ways. This section cover the options that you can pass when you create a <tt>has_and_belongs_to_many</tt> association. For example, an association with several options might look like this:</p></div>
2195
+ <div class="listingblock">
2196
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
2197
+ by Lorenzo Bettini
2198
+ http://www.lorenzobettini.it
2199
+ http://www.gnu.org/software/src-highlite -->
2200
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Parts <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
2201
+ has_and_belongs_to_many <span style="color: #990000">:</span>assemblies<span style="color: #990000">,</span> <span style="color: #990000">:</span>uniq <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">,</span> <span style="color: #990000">:</span>read_only <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
2202
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
2203
+ </tt></pre></div></div>
2204
+ <div class="para"><p>The <tt>has_and_belongs_to_many</tt> association supports these options:</p></div>
2205
+ <div class="ilist"><ul>
2206
+ <li>
2207
+ <p>
2208
+ <tt>:association_foreign_key</tt>
2209
+ </p>
2210
+ </li>
2211
+ <li>
2212
+ <p>
2213
+ <tt>:class_name</tt>
2214
+ </p>
2215
+ </li>
2216
+ <li>
2217
+ <p>
2218
+ <tt>:conditions</tt>
2219
+ </p>
2220
+ </li>
2221
+ <li>
2222
+ <p>
2223
+ <tt>:counter_sql</tt>
2224
+ </p>
2225
+ </li>
2226
+ <li>
2227
+ <p>
2228
+ <tt>:delete_sql</tt>
2229
+ </p>
2230
+ </li>
2231
+ <li>
2232
+ <p>
2233
+ <tt>:extend</tt>
2234
+ </p>
2235
+ </li>
2236
+ <li>
2237
+ <p>
2238
+ <tt>:finder_sql</tt>
2239
+ </p>
2240
+ </li>
2241
+ <li>
2242
+ <p>
2243
+ <tt>:foreign_key</tt>
2244
+ </p>
2245
+ </li>
2246
+ <li>
2247
+ <p>
2248
+ <tt>:group</tt>
2249
+ </p>
2250
+ </li>
2251
+ <li>
2252
+ <p>
2253
+ <tt>:include</tt>
2254
+ </p>
2255
+ </li>
2256
+ <li>
2257
+ <p>
2258
+ <tt>:insert_sql</tt>
2259
+ </p>
2260
+ </li>
2261
+ <li>
2262
+ <p>
2263
+ <tt>:join_table</tt>
2264
+ </p>
2265
+ </li>
2266
+ <li>
2267
+ <p>
2268
+ <tt>:limit</tt>
2269
+ </p>
2270
+ </li>
2271
+ <li>
2272
+ <p>
2273
+ <tt>:offset</tt>
2274
+ </p>
2275
+ </li>
2276
+ <li>
2277
+ <p>
2278
+ <tt>:order</tt>
2279
+ </p>
2280
+ </li>
2281
+ <li>
2282
+ <p>
2283
+ <tt>:readonly</tt>
2284
+ </p>
2285
+ </li>
2286
+ <li>
2287
+ <p>
2288
+ <tt>:select</tt>
2289
+ </p>
2290
+ </li>
2291
+ <li>
2292
+ <p>
2293
+ <tt>:uniq</tt>
2294
+ </p>
2295
+ </li>
2296
+ <li>
2297
+ <p>
2298
+ <tt>:validate</tt>
2299
+ </p>
2300
+ </li>
2301
+ </ul></div>
2302
+ <h5 id="_tt_association_foreign_key_tt"><tt>:association_foreign_key</tt></h5>
2303
+ <div class="para"><p>By convention, Rails guesses that the column in the join table used to hold the foreign key pointing to the other model is the name of that model with the suffix <tt>_id</tt> added. The <tt>:association_foreign_key</tt> option lets you set the name of the foreign key directly:</p></div>
2304
+ <div class="admonitionblock">
2305
+ <table><tr>
2306
+ <td class="icon">
2307
+ <img src="./images/icons/tip.png" alt="Tip" />
2308
+ </td>
2309
+ <td class="content">The <tt>:foreign_key</tt> and <tt>:association_foreign_key</tt> options are useful when setting up a many-to-many self-join. For example:</td>
2310
+ </tr></table>
2311
+ </div>
2312
+ <div class="listingblock">
2313
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
2314
+ by Lorenzo Bettini
2315
+ http://www.lorenzobettini.it
2316
+ http://www.gnu.org/software/src-highlite -->
2317
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> User <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
2318
+ has_and_belongs_to_many <span style="color: #990000">:</span>friends<span style="color: #990000">,</span> <span style="color: #990000">:</span>class_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"User"</span><span style="color: #990000">,</span>
2319
+ <span style="color: #990000">:</span>foreign_key <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"this_user_id"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>association_foreign_key <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"other_user_id"</span>
2320
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
2321
+ </tt></pre></div></div>
2322
+ <h5 id="_tt_class_name_tt_4"><tt>:class_name</tt></h5>
2323
+ <div class="para"><p>If the name of the other model cannot be derived from the association name, you can use the <tt>:class_name</tt> option to supply the model name. For example, if a part has many assemblies, but the actual name of the model containing assemblies is <tt>Gadget</tt>, you'd set things up this way:</p></div>
2324
+ <div class="listingblock">
2325
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
2326
+ by Lorenzo Bettini
2327
+ http://www.lorenzobettini.it
2328
+ http://www.gnu.org/software/src-highlite -->
2329
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Parts <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
2330
+ has_and_belongs_to_many <span style="color: #990000">:</span>assemblies<span style="color: #990000">,</span> <span style="color: #990000">:</span>class_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Gadget"</span>
2331
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
2332
+ </tt></pre></div></div>
2333
+ <h5 id="_tt_conditions_tt_4"><tt>:conditions</tt></h5>
2334
+ <div class="para"><p>The <tt>:conditions</tt> option lets you specify the conditions that the associated object must meet (in the syntax used by a SQL <tt>WHERE</tt> clause).</p></div>
2335
+ <div class="listingblock">
2336
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
2337
+ by Lorenzo Bettini
2338
+ http://www.lorenzobettini.it
2339
+ http://www.gnu.org/software/src-highlite -->
2340
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Parts <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
2341
+ has_and_belongs_to_many <span style="color: #990000">:</span>assemblies<span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"factory = 'Seattle'"</span>
2342
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
2343
+ </tt></pre></div></div>
2344
+ <div class="para"><p>You can also set conditions via a hash:</p></div>
2345
+ <div class="listingblock">
2346
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
2347
+ by Lorenzo Bettini
2348
+ http://www.lorenzobettini.it
2349
+ http://www.gnu.org/software/src-highlite -->
2350
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Parts <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
2351
+ has_and_belongs_to_many <span style="color: #990000">:</span>assemblies<span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>factory <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'Seattle'</span> <span style="color: #FF0000">}</span>
2352
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
2353
+ </tt></pre></div></div>
2354
+ <div class="para"><p>If you use a hash-style <tt>:conditions</tt> option, then record creation via this association will be automatically scoped using the hash. In this case, using <tt>@parts.assemblies.create</tt> or <tt>@parts.assemblies.build</tt> will create orders where the factory column has the value "Seattle".</p></div>
2355
+ <h5 id="_tt_counter_sql_tt_2"><tt>:counter_sql</tt></h5>
2356
+ <div class="para"><p>Normally Rails automatically generates the proper SQL to count the association members. With the <tt>:counter_sql</tt> option, you can specify a complete SQL statement to count them yourself.</p></div>
2357
+ <div class="admonitionblock">
2358
+ <table><tr>
2359
+ <td class="icon">
2360
+ <img src="./images/icons/note.png" alt="Note" />
2361
+ </td>
2362
+ <td class="content">If you specify <tt>:finder_sql</tt> but not <tt>:counter_sql</tt>, then the counter SQL will be generated by substituting <tt>SELECT COUNT(*) FROM</tt> for the <tt>SELECT &#8230; FROM</tt> clause of your <tt>:finder_sql</tt> statement.</td>
2363
+ </tr></table>
2364
+ </div>
2365
+ <h5 id="_tt_delete_sql_tt"><tt>:delete_sql</tt></h5>
2366
+ <div class="para"><p>Normally Rails automatically generates the proper SQL to remove links between the associated classes. With the <tt>:delete_sql</tt> option, you can specify a complete SQL statement to delete them yourself.</p></div>
2367
+ <h5 id="_tt_extend_tt_2"><tt>:extend</tt></h5>
2368
+ <div class="para"><p>The <tt>:extend</tt> option specifies a named module to extend the association proxy. Association extensions are discussed in detail later in this guide.</p></div>
2369
+ <h5 id="_tt_finder_sql_tt_2"><tt>:finder_sql</tt></h5>
2370
+ <div class="para"><p>Normally Rails automatically generates the proper SQL to fetch the association members. With the <tt>:finder_sql</tt> option, you can specify a complete SQL statement to fetch them yourself. If fetching objects requires complex multi-table SQL, this may be necessary.</p></div>
2371
+ <h5 id="_tt_foreign_key_tt_4"><tt>:foreign_key</tt></h5>
2372
+ <div class="para"><p>By convention, Rails guesses that the column in the join table used to hold the foreign key pointing to this model is the name of this model with the suffix <tt>_id</tt> added. The <tt>:foreign_key</tt> option lets you set the name of the foreign key directly:</p></div>
2373
+ <div class="listingblock">
2374
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
2375
+ by Lorenzo Bettini
2376
+ http://www.lorenzobettini.it
2377
+ http://www.gnu.org/software/src-highlite -->
2378
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> User <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
2379
+ has_and_belongs_to_many <span style="color: #990000">:</span>friends<span style="color: #990000">,</span> <span style="color: #990000">:</span>class_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"User"</span><span style="color: #990000">,</span>
2380
+ <span style="color: #990000">:</span>foreign_key <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"this_user_id"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>association_foreign_key <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"other_user_id"</span>
2381
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
2382
+ </tt></pre></div></div>
2383
+ <h5 id="_tt_group_tt_2"><tt>:group</tt></h5>
2384
+ <div class="para"><p>The <tt>:group</tt> option supplies an attribute name to group the result set by, using a <tt>GROUP BY</tt> clause in the finder SQL.</p></div>
2385
+ <div class="listingblock">
2386
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
2387
+ by Lorenzo Bettini
2388
+ http://www.lorenzobettini.it
2389
+ http://www.gnu.org/software/src-highlite -->
2390
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Parts <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
2391
+ has_and_belongs_to_many <span style="color: #990000">:</span>assemblies<span style="color: #990000">,</span> <span style="color: #990000">:</span>group <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"factory"</span>
2392
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
2393
+ </tt></pre></div></div>
2394
+ <h5 id="_tt_include_tt_4"><tt>:include</tt></h5>
2395
+ <div class="para"><p>You can use the :include option to specify second-order associations that should be eager-loaded when this association is used.</p></div>
2396
+ <h5 id="_tt_insert_sql_tt"><tt>:insert_sql</tt></h5>
2397
+ <div class="para"><p>Normally Rails automatically generates the proper SQL to create links between the associated classes. With the <tt>:insert_sql</tt> option, you can specify a complete SQL statement to insert them yourself.</p></div>
2398
+ <h5 id="_tt_join_table_tt"><tt>:join_table</tt></h5>
2399
+ <div class="para"><p>If the default name of the join table, based on lexical ordering, is not what you want, you can use the <tt>:join_table</tt> option to override the default.</p></div>
2400
+ <h5 id="_tt_limit_tt_2"><tt>:limit</tt></h5>
2401
+ <div class="para"><p>The <tt>:limit</tt> option lets you restrict the total number of objects that will be fetched through an association.</p></div>
2402
+ <div class="listingblock">
2403
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
2404
+ by Lorenzo Bettini
2405
+ http://www.lorenzobettini.it
2406
+ http://www.gnu.org/software/src-highlite -->
2407
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Parts <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
2408
+ has_and_belongs_to_many <span style="color: #990000">:</span>assemblies<span style="color: #990000">,</span> <span style="color: #990000">:</span>order <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"created_at DESC"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>limit <span style="color: #990000">=&gt;</span> <span style="color: #993399">50</span>
2409
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
2410
+ </tt></pre></div></div>
2411
+ <h5 id="_tt_offset_tt_2"><tt>:offset</tt></h5>
2412
+ <div class="para"><p>The <tt>:offset</tt> option lets you specify the starting offset for fetching objects via an association. For example, if you set <tt>:offset &#8658; 11</tt>, it will skip the first 10 records.</p></div>
2413
+ <h5 id="_tt_order_tt_3"><tt>:order</tt></h5>
2414
+ <div class="para"><p>The <tt>:order</tt> option dictates the order in which associated objects will be received (in the syntax used by a SQL <tt>ORDER BY</tt> clause).</p></div>
2415
+ <div class="listingblock">
2416
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
2417
+ by Lorenzo Bettini
2418
+ http://www.lorenzobettini.it
2419
+ http://www.gnu.org/software/src-highlite -->
2420
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Parts <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
2421
+ has_and_belongs_to_many <span style="color: #990000">:</span>assemblies<span style="color: #990000">,</span> <span style="color: #990000">:</span>order <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"assembly_name ASC"</span>
2422
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
2423
+ </tt></pre></div></div>
2424
+ <h5 id="_tt_readonly_tt_4"><tt>:readonly</tt></h5>
2425
+ <div class="para"><p>If you set the <tt>:readonly</tt> option to <tt>true</tt>, then the associated objects will be read-only when retrieved via the association.</p></div>
2426
+ <h5 id="_tt_select_tt_4"><tt>:select</tt></h5>
2427
+ <div class="para"><p>The <tt>:select</tt> option lets you override the SQL <tt>SELECT</tt> clause that is used to retrieve data about the associated objects. By default, Rails retrieves all columns.</p></div>
2428
+ <h5 id="_tt_uniq_tt_2"><tt>:uniq</tt></h5>
2429
+ <div class="para"><p>Specify the <tt>:uniq &#8658; true</tt> option to remove duplicates from the collection.</p></div>
2430
+ <h5 id="_tt_validate_tt_4"><tt>:validate</tt></h5>
2431
+ <div class="para"><p>If you set the <tt>:validate</tt> option to <tt>false</tt>, then associated objects will not be validated whenever you save this object. By default, this is <tt>true</tt>: associated objects will be validated when this object is saved.</p></div>
2432
+ <h4 id="_when_are_objects_saved_4">4.4.3. When are Objects Saved?</h4>
2433
+ <div class="para"><p>When you assign an object to a <tt>has_and_belongs_to_many</tt> association, that object is automatically saved (in order to update the join table). If you assign multiple objects in one statement, then they are all saved.</p></div>
2434
+ <div class="para"><p>If any of these saves fails due to validation errors, then the assignment statement returns <tt>false</tt> and the assignment itself is cancelled.</p></div>
2435
+ <div class="para"><p>If the parent object (the one declaring the <tt>has_and_belongs_to_many</tt> association) is unsaved (that is, <tt>new_record?</tt> returns <tt>true</tt>) then the child objects are not saved when they are added. All unsaved members of the association will automatically be saved when the parent is saved.</p></div>
2436
+ <div class="para"><p>If you want to assign an object to a <tt>has_and_belongs_to_many</tt> association without saving the object, use the <tt><em>collection</em>.build</tt> method.</p></div>
2437
+ <h3 id="_association_callbacks">4.5. Association Callbacks</h3>
2438
+ <div class="para"><p>Normal callbacks hook into the lifecycle of Active Record objects, allowing you to work with those objects at various points. For example, you can use a <tt>:before_save</tt> callback to cause something to happen just before an object is saved.</p></div>
2439
+ <div class="para"><p>Association callbacks are similar to normal callbacks, but they are triggered by events in the lifecycle of a collection. There are four available association callbacks:</p></div>
2440
+ <div class="ilist"><ul>
2441
+ <li>
2442
+ <p>
2443
+ <tt>before_add</tt>
2444
+ </p>
2445
+ </li>
2446
+ <li>
2447
+ <p>
2448
+ <tt>after_add</tt>
2449
+ </p>
2450
+ </li>
2451
+ <li>
2452
+ <p>
2453
+ <tt>before_remove</tt>
2454
+ </p>
2455
+ </li>
2456
+ <li>
2457
+ <p>
2458
+ <tt>after_remove</tt>
2459
+ </p>
2460
+ </li>
2461
+ </ul></div>
2462
+ <div class="para"><p>You define association callbacks by adding options to the association declaration. For example:</p></div>
2463
+ <div class="listingblock">
2464
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
2465
+ by Lorenzo Bettini
2466
+ http://www.lorenzobettini.it
2467
+ http://www.gnu.org/software/src-highlite -->
2468
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
2469
+ has_many <span style="color: #990000">:</span>orders<span style="color: #990000">,</span> <span style="color: #990000">:</span>before_add <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>check_credit_limit
2470
+
2471
+ <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> check_credit_limit<span style="color: #990000">(</span>order<span style="color: #990000">)</span>
2472
+ <span style="color: #990000">...</span>
2473
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
2474
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
2475
+ </tt></pre></div></div>
2476
+ <div class="para"><p>Rails passes the object being added or removed to the callback.</p></div>
2477
+ <div class="para"><p>You can stack callbacks on a single event by passing them as an array:</p></div>
2478
+ <div class="listingblock">
2479
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
2480
+ by Lorenzo Bettini
2481
+ http://www.lorenzobettini.it
2482
+ http://www.gnu.org/software/src-highlite -->
2483
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
2484
+ has_many <span style="color: #990000">:</span>orders<span style="color: #990000">,</span> <span style="color: #990000">:</span>before_add <span style="color: #990000">=&gt;</span> <span style="color: #990000">[:</span>check_credit_limit<span style="color: #990000">,</span> <span style="color: #990000">:</span>calculate_shipping_charges<span style="color: #990000">]</span>
2485
+
2486
+ <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> check_credit_limit<span style="color: #990000">(</span>order<span style="color: #990000">)</span>
2487
+ <span style="color: #990000">...</span>
2488
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
2489
+
2490
+ <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> calculate_shipping_charges<span style="color: #990000">(</span>order<span style="color: #990000">)</span>
2491
+ <span style="color: #990000">...</span>
2492
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
2493
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
2494
+ </tt></pre></div></div>
2495
+ <div class="para"><p>If a <tt>before_add</tt> callback throws an exception, the object does not get added to the collection. Similarly, if a <tt>before_remove</tt> callback throws an exception, the object does not get removed from the collection.</p></div>
2496
+ <h3 id="_association_extensions">4.6. Association Extensions</h3>
2497
+ <div class="para"><p>You're not limited to the functionality that Rails automatically builds into association proxy objects. You can also extend these objects through anonymous modules, adding new finders, creators, or other methods. For example:</p></div>
2498
+ <div class="listingblock">
2499
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
2500
+ by Lorenzo Bettini
2501
+ http://www.lorenzobettini.it
2502
+ http://www.gnu.org/software/src-highlite -->
2503
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
2504
+ has_many <span style="color: #990000">:</span>orders <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
2505
+ <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> find_by_order_prefix<span style="color: #990000">(</span>order_number<span style="color: #990000">)</span>
2506
+ find_by_region_id<span style="color: #990000">(</span>order_number<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">..</span><span style="color: #993399">2</span><span style="color: #990000">])</span>
2507
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
2508
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
2509
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
2510
+ </tt></pre></div></div>
2511
+ <div class="para"><p>If you have an extension that should be shared by many associations, you can use a named extension module. For example:</p></div>
2512
+ <div class="listingblock">
2513
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
2514
+ by Lorenzo Bettini
2515
+ http://www.lorenzobettini.it
2516
+ http://www.gnu.org/software/src-highlite -->
2517
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">module</span></span> FindRecentExtension
2518
+ <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> find_recent
2519
+ find<span style="color: #990000">(:</span>all<span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #990000">[</span><span style="color: #FF0000">"created_at &gt; ?"</span><span style="color: #990000">,</span> <span style="color: #993399">5</span><span style="color: #990000">.</span>days<span style="color: #990000">.</span>ago<span style="color: #990000">])</span>
2520
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
2521
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
2522
+
2523
+ <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
2524
+ has_many <span style="color: #990000">:</span>orders<span style="color: #990000">,</span> <span style="color: #990000">:</span>extend <span style="color: #990000">=&gt;</span> FindRecentExtension
2525
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
2526
+
2527
+ <span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Supplier <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
2528
+ has_many <span style="color: #990000">:</span>deliveries<span style="color: #990000">,</span> <span style="color: #990000">:</span>extend <span style="color: #990000">=&gt;</span> FindRecentExtension
2529
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
2530
+ </tt></pre></div></div>
2531
+ <div class="para"><p>To include more than one extension module in a single association, specify an array of names:</p></div>
2532
+ <div class="listingblock">
2533
+ <div class="content"><!-- Generator: GNU source-highlight 2.9
2534
+ by Lorenzo Bettini
2535
+ http://www.lorenzobettini.it
2536
+ http://www.gnu.org/software/src-highlite -->
2537
+ <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
2538
+ has_many <span style="color: #990000">:</span>orders<span style="color: #990000">,</span> <span style="color: #990000">:</span>extend <span style="color: #990000">=&gt;</span> <span style="color: #990000">[</span>FindRecentExtension<span style="color: #990000">,</span> FindActiveExtension<span style="color: #990000">]</span>
2539
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
2540
+ </tt></pre></div></div>
2541
+ <div class="para"><p>Extensions can refer to the internals of the association proxy using these three accessors:</p></div>
2542
+ <div class="ilist"><ul>
2543
+ <li>
2544
+ <p>
2545
+ <tt>proxy_owner</tt> returns the object that the association is a part of.
2546
+ </p>
2547
+ </li>
2548
+ <li>
2549
+ <p>
2550
+ <tt>proxy_reflection</tt> returns the reflection object that describes the association.
2551
+ </p>
2552
+ </li>
2553
+ <li>
2554
+ <p>
2555
+ <tt>proxy_target</tt> returns the associated object for <tt>belongs_to</tt> or <tt>has_one</tt>, or the collection of associated objects for <tt>has_many</tt> or <tt>has_and_belongs_to_many</tt>.
2556
+ </p>
2557
+ </li>
2558
+ </ul></div>
2559
+ </div>
2560
+ <h2 id="_changelog">5. Changelog</h2>
2561
+ <div class="sectionbody">
2562
+ <div class="para"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/11">Lighthouse ticket</a></p></div>
2563
+ <div class="ilist"><ul>
2564
+ <li>
2565
+ <p>
2566
+ September 28, 2008: Corrected <tt>has_many :through</tt> diagram, added polymorphic diagram, some reorganization by <a href="../authors.html#mgunderloy">Mike Gunderloy</a> . First release version.
2567
+ </p>
2568
+ </li>
2569
+ <li>
2570
+ <p>
2571
+ September 22, 2008: Added diagrams, misc. cleanup by <a href="../authors.html#mgunderloy">Mike Gunderloy</a> (not yet approved for publication)
2572
+ </p>
2573
+ </li>
2574
+ <li>
2575
+ <p>
2576
+ September 14, 2008: initial version by <a href="../authors.html#mgunderloy">Mike Gunderloy</a> (not yet approved for publication)
2577
+ </p>
2578
+ </li>
2579
+ </ul></div>
2580
+ </div>
2581
+
2582
+ </div>
2583
+ </div>
2584
+ </body>
2585
+ </html>