railties 3.1.1 → 3.1.2.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (189) hide show
  1. data/CHANGELOG.md +2382 -0
  2. data/guides/output/2_2_release_notes.html +565 -0
  3. data/guides/output/2_3_release_notes.html +713 -0
  4. data/guides/output/3_0_release_notes.html +652 -0
  5. data/guides/output/3_1_release_notes.html +670 -0
  6. data/guides/output/action_controller_overview.html +925 -0
  7. data/guides/output/action_mailer_basics.html +658 -0
  8. data/guides/output/action_view_overview.html +1471 -0
  9. data/guides/output/active_model_basics.html +349 -0
  10. data/guides/output/active_record_basics.html +364 -0
  11. data/guides/output/active_record_querying.html +1272 -0
  12. data/guides/output/active_record_validations_callbacks.html +1292 -0
  13. data/guides/output/active_resource_basics.html +252 -0
  14. data/guides/output/active_support_core_extensions.html +3374 -0
  15. data/guides/output/ajax_on_rails.html +412 -0
  16. data/guides/output/api_documentation_guidelines.html +317 -0
  17. data/guides/output/asset_pipeline.html +691 -0
  18. data/guides/output/association_basics.html +1742 -0
  19. data/guides/output/caching_with_rails.html +533 -0
  20. data/guides/output/command_line.html +662 -0
  21. data/guides/output/configuring.html +811 -0
  22. data/guides/output/contribute.html +216 -0
  23. data/guides/output/contributing_to_ruby_on_rails.html +465 -0
  24. data/guides/output/credits.html +210 -0
  25. data/guides/output/debugging_rails_applications.html +791 -0
  26. data/guides/output/engines.html +673 -0
  27. data/guides/output/form_helpers.html +850 -0
  28. data/guides/output/generators.html +725 -0
  29. data/guides/output/getting_started.html +1980 -0
  30. data/guides/output/i18n.html +1054 -0
  31. data/guides/output/images/belongs_to.png +0 -0
  32. data/guides/output/images/book_icon.gif +0 -0
  33. data/guides/output/images/bullet.gif +0 -0
  34. data/guides/output/images/challenge.png +0 -0
  35. data/guides/output/images/chapters_icon.gif +0 -0
  36. data/guides/output/images/check_bullet.gif +0 -0
  37. data/guides/output/images/credits_pic_blank.gif +0 -0
  38. data/guides/output/images/csrf.png +0 -0
  39. data/guides/output/images/customized_error_messages.png +0 -0
  40. data/guides/output/images/edge_badge.png +0 -0
  41. data/guides/output/images/error_messages.png +0 -0
  42. data/guides/output/images/feature_tile.gif +0 -0
  43. data/guides/output/images/footer_tile.gif +0 -0
  44. data/guides/output/images/fxn.png +0 -0
  45. data/guides/output/images/grey_bullet.gif +0 -0
  46. data/guides/output/images/habtm.png +0 -0
  47. data/guides/output/images/has_many.png +0 -0
  48. data/guides/output/images/has_many_through.png +0 -0
  49. data/guides/output/images/has_one.png +0 -0
  50. data/guides/output/images/has_one_through.png +0 -0
  51. data/guides/output/images/header_backdrop.png +0 -0
  52. data/guides/output/images/header_tile.gif +0 -0
  53. data/guides/output/images/i18n/demo_html_safe.png +0 -0
  54. data/guides/output/images/i18n/demo_localized_pirate.png +0 -0
  55. data/guides/output/images/i18n/demo_translated_en.png +0 -0
  56. data/guides/output/images/i18n/demo_translated_pirate.png +0 -0
  57. data/guides/output/images/i18n/demo_translation_missing.png +0 -0
  58. data/guides/output/images/i18n/demo_untranslated.png +0 -0
  59. data/guides/output/images/icons/README +5 -0
  60. data/guides/output/images/icons/callouts/1.png +0 -0
  61. data/guides/output/images/icons/callouts/10.png +0 -0
  62. data/guides/output/images/icons/callouts/11.png +0 -0
  63. data/guides/output/images/icons/callouts/12.png +0 -0
  64. data/guides/output/images/icons/callouts/13.png +0 -0
  65. data/guides/output/images/icons/callouts/14.png +0 -0
  66. data/guides/output/images/icons/callouts/15.png +0 -0
  67. data/guides/output/images/icons/callouts/2.png +0 -0
  68. data/guides/output/images/icons/callouts/3.png +0 -0
  69. data/guides/output/images/icons/callouts/4.png +0 -0
  70. data/guides/output/images/icons/callouts/5.png +0 -0
  71. data/guides/output/images/icons/callouts/6.png +0 -0
  72. data/guides/output/images/icons/callouts/7.png +0 -0
  73. data/guides/output/images/icons/callouts/8.png +0 -0
  74. data/guides/output/images/icons/callouts/9.png +0 -0
  75. data/guides/output/images/icons/caution.png +0 -0
  76. data/guides/output/images/icons/example.png +0 -0
  77. data/guides/output/images/icons/home.png +0 -0
  78. data/guides/output/images/icons/important.png +0 -0
  79. data/guides/output/images/icons/next.png +0 -0
  80. data/guides/output/images/icons/note.png +0 -0
  81. data/guides/output/images/icons/prev.png +0 -0
  82. data/guides/output/images/icons/tip.png +0 -0
  83. data/guides/output/images/icons/up.png +0 -0
  84. data/guides/output/images/icons/warning.png +0 -0
  85. data/guides/output/images/jaimeiniesta.jpg +0 -0
  86. data/guides/output/images/nav_arrow.gif +0 -0
  87. data/guides/output/images/polymorphic.png +0 -0
  88. data/guides/output/images/posts_index.png +0 -0
  89. data/guides/output/images/radar.png +0 -0
  90. data/guides/output/images/rails_guides_logo.gif +0 -0
  91. data/guides/output/images/rails_logo_remix.gif +0 -0
  92. data/guides/output/images/rails_welcome.png +0 -0
  93. data/guides/output/images/session_fixation.png +0 -0
  94. data/guides/output/images/tab_grey.gif +0 -0
  95. data/guides/output/images/tab_info.gif +0 -0
  96. data/guides/output/images/tab_note.gif +0 -0
  97. data/guides/output/images/tab_red.gif +0 -0
  98. data/guides/output/images/tab_yellow.gif +0 -0
  99. data/guides/output/images/tab_yellow.png +0 -0
  100. data/guides/output/images/validation_error_messages.png +0 -0
  101. data/guides/output/images/vijaydev.jpg +0 -0
  102. data/guides/output/index.html +300 -0
  103. data/guides/output/initialization.html +1087 -0
  104. data/guides/output/javascripts/guides.js +7 -0
  105. data/guides/output/javascripts/syntaxhighlighter/shBrushAS3.js +59 -0
  106. data/guides/output/javascripts/syntaxhighlighter/shBrushAppleScript.js +75 -0
  107. data/guides/output/javascripts/syntaxhighlighter/shBrushBash.js +59 -0
  108. data/guides/output/javascripts/syntaxhighlighter/shBrushCSharp.js +65 -0
  109. data/guides/output/javascripts/syntaxhighlighter/shBrushColdFusion.js +100 -0
  110. data/guides/output/javascripts/syntaxhighlighter/shBrushCpp.js +97 -0
  111. data/guides/output/javascripts/syntaxhighlighter/shBrushCss.js +91 -0
  112. data/guides/output/javascripts/syntaxhighlighter/shBrushDelphi.js +55 -0
  113. data/guides/output/javascripts/syntaxhighlighter/shBrushDiff.js +41 -0
  114. data/guides/output/javascripts/syntaxhighlighter/shBrushErlang.js +52 -0
  115. data/guides/output/javascripts/syntaxhighlighter/shBrushGroovy.js +67 -0
  116. data/guides/output/javascripts/syntaxhighlighter/shBrushJScript.js +52 -0
  117. data/guides/output/javascripts/syntaxhighlighter/shBrushJava.js +57 -0
  118. data/guides/output/javascripts/syntaxhighlighter/shBrushJavaFX.js +58 -0
  119. data/guides/output/javascripts/syntaxhighlighter/shBrushPerl.js +72 -0
  120. data/guides/output/javascripts/syntaxhighlighter/shBrushPhp.js +88 -0
  121. data/guides/output/javascripts/syntaxhighlighter/shBrushPlain.js +33 -0
  122. data/guides/output/javascripts/syntaxhighlighter/shBrushPowerShell.js +74 -0
  123. data/guides/output/javascripts/syntaxhighlighter/shBrushPython.js +64 -0
  124. data/guides/output/javascripts/syntaxhighlighter/shBrushRuby.js +55 -0
  125. data/guides/output/javascripts/syntaxhighlighter/shBrushSass.js +94 -0
  126. data/guides/output/javascripts/syntaxhighlighter/shBrushScala.js +51 -0
  127. data/guides/output/javascripts/syntaxhighlighter/shBrushSql.js +66 -0
  128. data/guides/output/javascripts/syntaxhighlighter/shBrushVb.js +56 -0
  129. data/guides/output/javascripts/syntaxhighlighter/shBrushXml.js +69 -0
  130. data/guides/output/javascripts/syntaxhighlighter/shCore.js +17 -0
  131. data/guides/output/layout.html +312 -0
  132. data/guides/output/layouts_and_rendering.html +1257 -0
  133. data/guides/output/migrations.html +751 -0
  134. data/guides/output/nested_model_forms.html +350 -0
  135. data/guides/output/performance_testing.html +858 -0
  136. data/guides/output/plugins.html +590 -0
  137. data/guides/output/rails_application_templates.html +368 -0
  138. data/guides/output/rails_on_rack.html +408 -0
  139. data/guides/output/routing.html +1246 -0
  140. data/guides/output/ruby_on_rails_guides_guidelines.html +218 -0
  141. data/guides/output/security.html +968 -0
  142. data/guides/output/stylesheets/fixes.css +16 -0
  143. data/guides/output/stylesheets/main.css +445 -0
  144. data/guides/output/stylesheets/print.css +52 -0
  145. data/guides/output/stylesheets/reset.css +43 -0
  146. data/guides/output/stylesheets/style.css +13 -0
  147. data/guides/output/stylesheets/syntaxhighlighter/shCore.css +226 -0
  148. data/guides/output/stylesheets/syntaxhighlighter/shCoreDefault.css +328 -0
  149. data/guides/output/stylesheets/syntaxhighlighter/shCoreDjango.css +331 -0
  150. data/guides/output/stylesheets/syntaxhighlighter/shCoreEclipse.css +339 -0
  151. data/guides/output/stylesheets/syntaxhighlighter/shCoreEmacs.css +324 -0
  152. data/guides/output/stylesheets/syntaxhighlighter/shCoreFadeToGrey.css +328 -0
  153. data/guides/output/stylesheets/syntaxhighlighter/shCoreMDUltra.css +324 -0
  154. data/guides/output/stylesheets/syntaxhighlighter/shCoreMidnight.css +324 -0
  155. data/guides/output/stylesheets/syntaxhighlighter/shCoreRDark.css +324 -0
  156. data/guides/output/stylesheets/syntaxhighlighter/shThemeDefault.css +117 -0
  157. data/guides/output/stylesheets/syntaxhighlighter/shThemeDjango.css +120 -0
  158. data/guides/output/stylesheets/syntaxhighlighter/shThemeEclipse.css +128 -0
  159. data/guides/output/stylesheets/syntaxhighlighter/shThemeEmacs.css +113 -0
  160. data/guides/output/stylesheets/syntaxhighlighter/shThemeFadeToGrey.css +117 -0
  161. data/guides/output/stylesheets/syntaxhighlighter/shThemeMDUltra.css +113 -0
  162. data/guides/output/stylesheets/syntaxhighlighter/shThemeMidnight.css +113 -0
  163. data/guides/output/stylesheets/syntaxhighlighter/shThemeRDark.css +113 -0
  164. data/guides/output/stylesheets/syntaxhighlighter/shThemeRailsGuides.css +116 -0
  165. data/guides/output/testing.html +1182 -0
  166. data/guides/source/active_record_validations_callbacks.textile +3 -3
  167. data/guides/source/active_support_core_extensions.textile +2 -2
  168. data/guides/source/asset_pipeline.textile +7 -5
  169. data/guides/source/association_basics.textile +2 -4
  170. data/guides/source/command_line.textile +1 -1
  171. data/guides/source/configuring.textile +4 -2
  172. data/guides/source/contributing_to_ruby_on_rails.textile +6 -6
  173. data/guides/source/initialization.textile +2 -2
  174. data/guides/source/performance_testing.textile +1 -1
  175. data/guides/source/routing.textile +1 -1
  176. data/guides/source/security.textile +2 -2
  177. data/lib/rails/commands/server.rb +1 -1
  178. data/lib/rails/engine.rb +1 -1
  179. data/lib/rails/generators/app_base.rb +2 -1
  180. data/lib/rails/generators/base.rb +3 -3
  181. data/lib/rails/generators/rails/app/templates/gitignore +15 -5
  182. data/lib/rails/rack.rb +0 -1
  183. data/lib/rails/tasks/documentation.rake +7 -7
  184. data/lib/rails/tasks/misc.rake +0 -2
  185. data/lib/rails/test_unit/testing.rake +2 -0
  186. data/lib/rails/version.rb +2 -2
  187. metadata +495 -326
  188. data/CHANGELOG +0 -2371
  189. data/lib/rails/rack/content_length.rb +0 -38
@@ -0,0 +1,349 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
2
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3
+
4
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
5
+ <head>
6
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
7
+
8
+ <title>Ruby on Rails Guides: Active Model Basics</title>
9
+
10
+ <link rel="stylesheet" type="text/css" href="stylesheets/style.css" />
11
+ <link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print" />
12
+
13
+ <link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shCore.css" />
14
+ <link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shThemeRailsGuides.css" />
15
+
16
+ <link rel="stylesheet" type="text/css" href="stylesheets/fixes.css" />
17
+ </head>
18
+ <body class="guide">
19
+ <div id="topNav">
20
+ <div class="wrapper">
21
+ <strong>More at <a href="http://rubyonrails.org/">rubyonrails.org:</a> </strong>
22
+ <a href="http://rubyonrails.org/">Overview</a> |
23
+ <a href="http://rubyonrails.org/download">Download</a> |
24
+ <a href="http://rubyonrails.org/deploy">Deploy</a> |
25
+ <a href="https://github.com/rails/rails">Code</a> |
26
+ <a href="http://rubyonrails.org/screencasts">Screencasts</a> |
27
+ <a href="http://rubyonrails.org/documentation">Documentation</a> |
28
+ <a href="http://rubyonrails.org/ecosystem">Ecosystem</a> |
29
+ <a href="http://rubyonrails.org/community">Community</a> |
30
+ <a href="http://weblog.rubyonrails.org/">Blog</a>
31
+ </div>
32
+ </div>
33
+ <div id="header">
34
+ <div class="wrapper clearfix">
35
+ <h1><a href="index.html" title="Return to home page">Guides.rubyonrails.org</a></h1>
36
+ <p class="hide"><a href="#mainCol">Skip navigation</a>.</p>
37
+ <ul class="nav">
38
+ <li><a href="index.html">Home</a></li>
39
+ <li class="index"><a href="index.html" onclick="guideMenu(); return false;" id="guidesMenu">Guides Index</a>
40
+ <div id="guides" class="clearfix" style="display: none;">
41
+ <hr />
42
+ <dl class="L">
43
+ <dt>Start Here</dt>
44
+ <dd><a href="getting_started.html">Getting Started with Rails</a></dd>
45
+ <dt>Models</dt>
46
+ <dd><a href="migrations.html">Rails Database Migrations</a></dd>
47
+ <dd><a href="active_record_validations_callbacks.html">Active Record Validations and Callbacks</a></dd>
48
+ <dd><a href="association_basics.html">Active Record Associations</a></dd>
49
+ <dd><a href="active_record_querying.html">Active Record Query Interface</a></dd>
50
+ <dt>Views</dt>
51
+ <dd><a href="layouts_and_rendering.html">Layouts and Rendering in Rails</a></dd>
52
+ <dd><a href="form_helpers.html">Action View Form Helpers</a></dd>
53
+ <dt>Controllers</dt>
54
+ <dd><a href="action_controller_overview.html">Action Controller Overview</a></dd>
55
+ <dd><a href="routing.html">Rails Routing from the Outside In</a></dd>
56
+ </dl>
57
+ <dl class="R">
58
+ <dt>Digging Deeper</dt>
59
+ <dd><a href="active_support_core_extensions.html">Active Support Core Extensions</a></dd>
60
+ <dd><a href="i18n.html">Rails Internationalization API</a></dd>
61
+ <dd><a href="action_mailer_basics.html">Action Mailer Basics</a></dd>
62
+ <dd><a href="testing.html">Testing Rails Applications</a></dd>
63
+ <dd><a href="security.html">Securing Rails Applications</a></dd>
64
+ <dd><a href="debugging_rails_applications.html">Debugging Rails Applications</a></dd>
65
+ <dd><a href="performance_testing.html">Performance Testing Rails Applications</a></dd>
66
+ <dd><a href="configuring.html">Configuring Rails Applications</a></dd>
67
+ <dd><a href="command_line.html">Rails Command Line Tools and Rake Tasks</a></dd>
68
+ <dd><a href="caching_with_rails.html">Caching with Rails</a></dd>
69
+ <dd><a href="asset_pipeline.html">Asset Pipeline</a></dd>
70
+
71
+ <dt>Extending Rails</dt>
72
+ <dd><a href="plugins.html">The Basics of Creating Rails Plugins</a></dd>
73
+ <dd><a href="rails_on_rack.html">Rails on Rack</a></dd>
74
+ <dd><a href="generators.html">Creating and Customizing Rails Generators</a></dd>
75
+
76
+ <dt>Contributing to Ruby on Rails</dt>
77
+ <dd><a href="contributing_to_ruby_on_rails.html">Contributing to Ruby on Rails</a></dd>
78
+ <dd><a href="api_documentation_guidelines.html">API Documentation Guidelines</a></dd>
79
+ <dd><a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a></dd>
80
+
81
+ <dt>Release Notes</dt>
82
+ <dd><a href="3_1_release_notes.html">Ruby on Rails 3.1 Release Notes</a></dd>
83
+ <dd><a href="3_0_release_notes.html">Ruby on Rails 3.0 Release Notes</a></dd>
84
+ <dd><a href="2_3_release_notes.html">Ruby on Rails 2.3 Release Notes</a></dd>
85
+ <dd><a href="2_2_release_notes.html">Ruby on Rails 2.2 Release Notes</a></dd>
86
+ </dl>
87
+ </div>
88
+ </li>
89
+ <li><a href="contributing_to_ruby_on_rails.html">Contribute</a></li>
90
+ <li><a href="credits.html">Credits</a></li>
91
+ </ul>
92
+ </div>
93
+ </div>
94
+ <hr class="hide" />
95
+
96
+ <div id="feature">
97
+ <div class="wrapper">
98
+ <h2>Active Model Basics</h2>
99
+ <p>This guide should provide you with all you need to get started using model classes. Active Model allow for Action Pack helpers to interact with non-ActiveRecord models. Active Model also helps building custom ORMs for use outside of the Rails framework.</p>
100
+
101
+ <div id="subCol">
102
+ <h3 class="chapter"><img src="images/chapters_icon.gif" alt="" />Chapters</h3>
103
+ <ol class="chapters">
104
+ <li><a href="#introduction">Introduction</a><ul><li><a href="#attributemethods">AttributeMethods</a></li> <li><a href="#callbacks">Callbacks</a></li> <li><a href="#conversion">Conversion</a></li> <li><a href="#dirty">Dirty</a></li> <li><a href="#validations">Validations</a></li></ul></li></ol></div>
105
+ </div>
106
+ </div>
107
+
108
+ <div id="container">
109
+ <div class="wrapper">
110
+ <div id="mainCol">
111
+ <div class="warning"><p>This Guide is based on Rails 3.0. Some of the code shown here will not work in earlier versions of Rails.</p></div>
112
+ <h3 id="introduction">1 Introduction</h3>
113
+ <p>Active Model is a library containing various modules used in developing frameworks that need to interact with the Rails Action Pack library. Active Model provides a known set of interfaces for usage in classes. Some of modules are explained below -</p>
114
+ <h4 id="attributemethods">1.1 AttributeMethods</h4>
115
+ <p>AttributeMethods module can add custom prefixes and suffixes on methods of a class. It is used by defining the prefixes and suffixes, which methods on the object will use them.</p>
116
+ <div class="code_container">
117
+ <pre class="brush: ruby; gutter: false; toolbar: false">
118
+ class Person
119
+ include ActiveModel::AttributeMethods
120
+
121
+ attribute_method_prefix 'reset_'
122
+ attribute_method_suffix '_highest?'
123
+ define_attribute_methods ['age']
124
+
125
+ attr_accessor :age
126
+
127
+ private
128
+ def reset_attribute(attribute)
129
+ send(&quot;#{attribute}=&quot;, 0)
130
+ end
131
+
132
+ def attribute_highest?(attribute)
133
+ send(attribute) &gt; 100 ? true : false
134
+ end
135
+
136
+ end
137
+
138
+ person = Person.new
139
+ person.age = 110
140
+ person.age_highest? # true
141
+ person.reset_age # 0
142
+ person.age_highest? # false
143
+ </pre>
144
+ </div>
145
+ <h4 id="callbacks">1.2 Callbacks</h4>
146
+ <p>Callbacks gives Active Record style callbacks. This provides the ability to define the callbacks and those will run at appropriate time. After defining a callbacks you can wrap with before, after and around custom methods.</p>
147
+ <div class="code_container">
148
+ <pre class="brush: ruby; gutter: false; toolbar: false">
149
+ class Person
150
+ extend ActiveModel::Callbacks
151
+
152
+ define_model_callbacks :update
153
+
154
+ before_update :reset_me
155
+
156
+ def update
157
+ _run_update_callbacks do
158
+ # This will call when we are trying to call update on object.
159
+ end
160
+ end
161
+
162
+ def reset_me
163
+ # This method will call when you are calling update on object as a before_update callback as defined.
164
+ end
165
+ end
166
+ </pre>
167
+ </div>
168
+ <h4 id="conversion">1.3 Conversion</h4>
169
+ <p>If a class defines persisted? and id methods then you can include Conversion module in that class and you can able to call Rails conversion methods to objects of that class.</p>
170
+ <div class="code_container">
171
+ <pre class="brush: ruby; gutter: false; toolbar: false">
172
+ class Person
173
+ include ActiveModel::Conversion
174
+
175
+ def persisted?
176
+ false
177
+ end
178
+
179
+ def id
180
+ nil
181
+ end
182
+ end
183
+
184
+ person = Person.new
185
+ person.to_model == person #=&gt; true
186
+ person.to_key #=&gt; nil
187
+ person.to_param #=&gt; nil
188
+ </pre>
189
+ </div>
190
+ <h4 id="dirty">1.4 Dirty</h4>
191
+ <p>An object becomes dirty when an object is gone through one or more changes to its attributes and not yet saved. This gives the ability to check whether an object has been changed or not. It also has attribute based accessor methods. Lets consider a Person class with attributes first_name and last_name</p>
192
+ <div class="code_container">
193
+ <pre class="brush: ruby; gutter: false; toolbar: false">
194
+ require 'rubygems'
195
+ require 'active_model'
196
+
197
+ class Person
198
+ include ActiveModel::Dirty
199
+ define_attribute_methods [:first_name, :last_name]
200
+
201
+ def first_name
202
+ @first_name
203
+ end
204
+
205
+ def first_name=(value)
206
+ first_name_will_change!
207
+ @first_name = value
208
+ end
209
+
210
+ def last_name
211
+ @last_name
212
+ end
213
+
214
+ def last_name=(value)
215
+ last_name_will_change!
216
+ @last_name = value
217
+ end
218
+
219
+ def save
220
+ @previously_changed = changes
221
+ end
222
+
223
+ end
224
+ </pre>
225
+ </div>
226
+ <h5 id="querying-object-directly-for-its-list-of-all-changed-attributes">1.4.1 Querying object directly for its list of all changed attributes.</h5>
227
+ <div class="code_container">
228
+ <pre class="brush: ruby; gutter: false; toolbar: false">
229
+ person = Person.new
230
+ person.first_name = &quot;First Name&quot;
231
+
232
+ person.first_name #=&gt; &quot;First Name&quot;
233
+ person.first_name = &quot;First Name Changed&quot;
234
+
235
+ person.changed? #=&gt; true
236
+
237
+ #returns an list of fields arry which all has been changed before saved.
238
+ person.changed #=&gt; [&quot;first_name&quot;]
239
+
240
+ #returns a hash of the fields that have changed with their original values.
241
+ person.changed_attributes #=&gt; {&quot;first_name&quot; =&gt; &quot;First Name Changed&quot;}
242
+
243
+ #returns a hash of changes, with the attribute names as the keys, and the values will be an array of the old and new value for that field.
244
+ person.changes #=&gt; {&quot;first_name&quot; =&gt; [&quot;First Name&quot;,&quot;First Name Changed&quot;]}
245
+ </pre>
246
+ </div>
247
+ <h5 id="attribute-based-accessor-methods">1.4.2 Attribute based accessor methods</h5>
248
+ <p>Track whether the particular attribute has been changed or not.</p>
249
+ <div class="code_container">
250
+ <pre class="brush: ruby; gutter: false; toolbar: false">
251
+ #attr_name_changed?
252
+ person.first_name #=&gt; &quot;First Name&quot;
253
+
254
+ #assign some other value to first_name attribute
255
+ person.first_name = &quot;First Name 1&quot;
256
+
257
+ person.first_name_changed? #=&gt; true
258
+ </pre>
259
+ </div>
260
+ <p>Track what was the previous value of the attribute.</p>
261
+ <div class="code_container">
262
+ <pre class="brush: ruby; gutter: false; toolbar: false">
263
+ #attr_name_was accessor
264
+ person.first_name_was #=&gt; &quot;First Name&quot;
265
+ </pre>
266
+ </div>
267
+ <p>Track both previous and current value of the changed attribute. Returns an array if changed else returns nil</p>
268
+ <div class="code_container">
269
+ <pre class="brush: ruby; gutter: false; toolbar: false">
270
+ #attr_name_change
271
+ person.first_name_change #=&gt; [&quot;First Name&quot;, &quot;First Name 1&quot;]
272
+ person.last_name_change #=&gt; nil
273
+ </pre>
274
+ </div>
275
+ <h4 id="validations">1.5 Validations</h4>
276
+ <p>Validations module adds the ability to class objects to validate them in Active Record style.</p>
277
+ <div class="code_container">
278
+ <pre class="brush: ruby; gutter: false; toolbar: false">
279
+ class Person
280
+ include ActiveModel::Validations
281
+
282
+ attr_accessor :name, :email, :token
283
+
284
+ validates :name, :presence =&gt; true
285
+ validates_format_of :email, :with =&gt; /^([^\s]+)((?:[-a-z0-9]\.)[a-z]{2,})$/i
286
+ validates! :token, :presence =&gt; true
287
+
288
+ end
289
+
290
+ person = Person.new(:token =&gt; &quot;2b1f325&quot;)
291
+ person.valid? #=&gt; false
292
+ person.name = 'vishnu'
293
+ person.email = 'me'
294
+ person.valid? #=&gt; false
295
+ person.email = 'me@vishnuatrai.com'
296
+ person.valid? #=&gt; true
297
+ person.token = nil
298
+ person.valid? #=&gt; raises ActiveModel::StrictValidationFailed
299
+ </pre>
300
+ </div>
301
+
302
+ <h3>Feedback</h3>
303
+ <p>
304
+ You're encouraged to help improve the quality of this guide.
305
+ </p>
306
+ <p>
307
+ If you see any typos or factual errors you are confident to
308
+ patch, please clone <a href="https://github.com/lifo/docrails">docrails</a>
309
+ and push the change yourself. That branch of Rails has public write access.
310
+ Commits are still reviewed, but that happens after you've submitted your
311
+ contribution. <a href="https://github.com/lifo/docrails">docrails</a> is
312
+ cross-merged with master periodically.
313
+ </p>
314
+ <p>
315
+ You may also find incomplete content, or stuff that is not up to date.
316
+ Please do add any missing documentation for master. Check the
317
+ <a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a>
318
+ for style and conventions.
319
+ </p>
320
+ <p>
321
+ If for whatever reason you spot something to fix but cannot patch it yourself, please
322
+ <a href="https://github.com/rails/rails/issues">open an issue</a>.
323
+ </p>
324
+ <p>And last but not least, any kind of discussion regarding Ruby on Rails
325
+ documentation is very welcome in the <a href="http://groups.google.com/group/rubyonrails-docs">rubyonrails-docs mailing list</a>.
326
+ </p>
327
+ </div>
328
+ </div>
329
+ </div>
330
+
331
+ <hr class="hide" />
332
+ <div id="footer">
333
+ <div class="wrapper">
334
+ <p>This work is licensed under a <a href="http://creativecommons.org/licenses/by-sa/3.0/">Creative Commons Attribution-Share Alike 3.0</a> License</p>
335
+ <p>"Rails", "Ruby on Rails", and the Rails logo are trademarks of David Heinemeier Hansson. All rights reserved.</p>
336
+ </div>
337
+ </div>
338
+
339
+ <script type="text/javascript" src="javascripts/guides.js"></script>
340
+ <script type="text/javascript" src="javascripts/syntaxhighlighter/shCore.js"></script>
341
+ <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushRuby.js"></script>
342
+ <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushXml.js"></script>
343
+ <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushSql.js"></script>
344
+ <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushPlain.js"></script>
345
+ <script type="text/javascript">
346
+ SyntaxHighlighter.all()
347
+ </script>
348
+ </body>
349
+ </html>
@@ -0,0 +1,364 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
2
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3
+
4
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
5
+ <head>
6
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
7
+
8
+ <title>Ruby on Rails Guides: Active Record Basics</title>
9
+
10
+ <link rel="stylesheet" type="text/css" href="stylesheets/style.css" />
11
+ <link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print" />
12
+
13
+ <link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shCore.css" />
14
+ <link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shThemeRailsGuides.css" />
15
+
16
+ <link rel="stylesheet" type="text/css" href="stylesheets/fixes.css" />
17
+ </head>
18
+ <body class="guide">
19
+ <div id="topNav">
20
+ <div class="wrapper">
21
+ <strong>More at <a href="http://rubyonrails.org/">rubyonrails.org:</a> </strong>
22
+ <a href="http://rubyonrails.org/">Overview</a> |
23
+ <a href="http://rubyonrails.org/download">Download</a> |
24
+ <a href="http://rubyonrails.org/deploy">Deploy</a> |
25
+ <a href="https://github.com/rails/rails">Code</a> |
26
+ <a href="http://rubyonrails.org/screencasts">Screencasts</a> |
27
+ <a href="http://rubyonrails.org/documentation">Documentation</a> |
28
+ <a href="http://rubyonrails.org/ecosystem">Ecosystem</a> |
29
+ <a href="http://rubyonrails.org/community">Community</a> |
30
+ <a href="http://weblog.rubyonrails.org/">Blog</a>
31
+ </div>
32
+ </div>
33
+ <div id="header">
34
+ <div class="wrapper clearfix">
35
+ <h1><a href="index.html" title="Return to home page">Guides.rubyonrails.org</a></h1>
36
+ <p class="hide"><a href="#mainCol">Skip navigation</a>.</p>
37
+ <ul class="nav">
38
+ <li><a href="index.html">Home</a></li>
39
+ <li class="index"><a href="index.html" onclick="guideMenu(); return false;" id="guidesMenu">Guides Index</a>
40
+ <div id="guides" class="clearfix" style="display: none;">
41
+ <hr />
42
+ <dl class="L">
43
+ <dt>Start Here</dt>
44
+ <dd><a href="getting_started.html">Getting Started with Rails</a></dd>
45
+ <dt>Models</dt>
46
+ <dd><a href="migrations.html">Rails Database Migrations</a></dd>
47
+ <dd><a href="active_record_validations_callbacks.html">Active Record Validations and Callbacks</a></dd>
48
+ <dd><a href="association_basics.html">Active Record Associations</a></dd>
49
+ <dd><a href="active_record_querying.html">Active Record Query Interface</a></dd>
50
+ <dt>Views</dt>
51
+ <dd><a href="layouts_and_rendering.html">Layouts and Rendering in Rails</a></dd>
52
+ <dd><a href="form_helpers.html">Action View Form Helpers</a></dd>
53
+ <dt>Controllers</dt>
54
+ <dd><a href="action_controller_overview.html">Action Controller Overview</a></dd>
55
+ <dd><a href="routing.html">Rails Routing from the Outside In</a></dd>
56
+ </dl>
57
+ <dl class="R">
58
+ <dt>Digging Deeper</dt>
59
+ <dd><a href="active_support_core_extensions.html">Active Support Core Extensions</a></dd>
60
+ <dd><a href="i18n.html">Rails Internationalization API</a></dd>
61
+ <dd><a href="action_mailer_basics.html">Action Mailer Basics</a></dd>
62
+ <dd><a href="testing.html">Testing Rails Applications</a></dd>
63
+ <dd><a href="security.html">Securing Rails Applications</a></dd>
64
+ <dd><a href="debugging_rails_applications.html">Debugging Rails Applications</a></dd>
65
+ <dd><a href="performance_testing.html">Performance Testing Rails Applications</a></dd>
66
+ <dd><a href="configuring.html">Configuring Rails Applications</a></dd>
67
+ <dd><a href="command_line.html">Rails Command Line Tools and Rake Tasks</a></dd>
68
+ <dd><a href="caching_with_rails.html">Caching with Rails</a></dd>
69
+ <dd><a href="asset_pipeline.html">Asset Pipeline</a></dd>
70
+
71
+ <dt>Extending Rails</dt>
72
+ <dd><a href="plugins.html">The Basics of Creating Rails Plugins</a></dd>
73
+ <dd><a href="rails_on_rack.html">Rails on Rack</a></dd>
74
+ <dd><a href="generators.html">Creating and Customizing Rails Generators</a></dd>
75
+
76
+ <dt>Contributing to Ruby on Rails</dt>
77
+ <dd><a href="contributing_to_ruby_on_rails.html">Contributing to Ruby on Rails</a></dd>
78
+ <dd><a href="api_documentation_guidelines.html">API Documentation Guidelines</a></dd>
79
+ <dd><a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a></dd>
80
+
81
+ <dt>Release Notes</dt>
82
+ <dd><a href="3_1_release_notes.html">Ruby on Rails 3.1 Release Notes</a></dd>
83
+ <dd><a href="3_0_release_notes.html">Ruby on Rails 3.0 Release Notes</a></dd>
84
+ <dd><a href="2_3_release_notes.html">Ruby on Rails 2.3 Release Notes</a></dd>
85
+ <dd><a href="2_2_release_notes.html">Ruby on Rails 2.2 Release Notes</a></dd>
86
+ </dl>
87
+ </div>
88
+ </li>
89
+ <li><a href="contributing_to_ruby_on_rails.html">Contribute</a></li>
90
+ <li><a href="credits.html">Credits</a></li>
91
+ </ul>
92
+ </div>
93
+ </div>
94
+ <hr class="hide" />
95
+
96
+ <div id="feature">
97
+ <div class="wrapper">
98
+ <h2>Active Record Basics</h2>
99
+ <p>This guide is an introduction to Active Record. After reading this guide we hope that you&#8217;ll learn:</p>
100
+ <ul>
101
+ <li>What Object Relational Mapping and Active Record are and how they are used in Rails</li>
102
+ <li>How Active Record fits into the Model-View-Controller paradigm</li>
103
+ <li>How to use Active Record models to manipulate data stored in a relational database</li>
104
+ <li>Active Record schema naming conventions</li>
105
+ <li>The concepts of database migrations, validations and callbacks</li>
106
+ </ul>
107
+
108
+ <div id="subCol">
109
+ <h3 class="chapter"><img src="images/chapters_icon.gif" alt="" />Chapters</h3>
110
+ <ol class="chapters">
111
+ <li><a href="#what-is-active-record">What is Active Record?</a><ul><li><a href="#the-active-record-pattern">The Active Record Pattern</a></li> <li><a href="#object-relational-mapping">Object Relational Mapping</a></li> <li><a href="#active-record-as-an-orm-framework">Active Record as an <span class="caps">ORM</span> Framework</a></li></ul></li><li><a href="#convention-over-configuration-in-active-record">Convention over Configuration in Active Record</a><ul><li><a href="#naming-conventions">Naming Conventions</a></li> <li><a href="#schema-conventions">Schema Conventions</a></li></ul></li><li><a href="#creating-active-record-models">Creating Active Record Models</a></li><li><a href="#overriding-the-naming-conventions">Overriding the Naming Conventions</a></li><li><a href="#crud-reading-and-writing-data"><span class="caps">CRUD</span>: Reading and Writing Data</a><ul><li><a href="#create">Create</a></li> <li><a href="#read">Read</a></li> <li><a href="#update">Update</a></li> <li><a href="#delete">Delete</a></li></ul></li><li><a href="#validations">Validations</a></li><li><a href="#callbacks">Callbacks</a></li><li><a href="#migrations">Migrations</a></li></ol></div>
112
+ </div>
113
+ </div>
114
+
115
+ <div id="container">
116
+ <div class="wrapper">
117
+ <div id="mainCol">
118
+ <h3 id="what-is-active-record">1 What is Active Record?</h3>
119
+ <p>Active Record is the M in <a href="getting_started.html#the-mvc-architecture"><span class="caps">MVC</span></a> &#8211; the model &#8211; which is the layer of the system responsible for representing business data and logic. Active Record facilitates the creation and use of business objects whose data requires persistent storage to a database. It is an implementation of the Active Record pattern which itself is a description of an Object Relational Mapping system.</p>
120
+ <h4 id="the-active-record-pattern">1.1 The Active Record Pattern</h4>
121
+ <p>Active Record was described by Martin Fowler in his book <em>Patterns of Enterprise Application Architecture</em>. In Active Record, objects carry both persistent data and behavior which operates on that data. Active Record takes the opinion that ensuring data access logic is part of the object will educate users of that object on how to write to and read from the database.</p>
122
+ <h4 id="object-relational-mapping">1.2 Object Relational Mapping</h4>
123
+ <p>Object-Relational Mapping, commonly referred to as its abbreviation <span class="caps">ORM</span>, is a technique that connects the rich objects of an application to tables in a relational database management system. Using <span class="caps">ORM</span>, the properties and relationships of the objects in an application can be easily stored and retrieved from a database without writing <span class="caps">SQL</span> statements directly and with less overall database access code.</p>
124
+ <h4 id="active-record-as-an-orm-framework">1.3 Active Record as an <span class="caps">ORM</span> Framework</h4>
125
+ <p>Active Record gives us several mechanisms, the most important being the ability to:</p>
126
+ <ul>
127
+ <li>Represent models and their data</li>
128
+ <li>Represent associations between these models</li>
129
+ <li>Represent inheritance hierarchies through related models</li>
130
+ <li>Validate models before they get persisted to the database</li>
131
+ <li>Perform database operations in an object-oriented fashion.</li>
132
+ </ul>
133
+ <h3 id="convention-over-configuration-in-active-record">2 Convention over Configuration in Active Record</h3>
134
+ <p>When writing applications using other programming languages or frameworks, it may be necessary to write a lot of configuration code. This is particularly true for <span class="caps">ORM</span> frameworks in general. However, if you follow the conventions adopted by Rails, you&#8217;ll need to write very little configuration (in some case no configuration at all) when creating Active Record models. The idea is that if you configure your applications in the very same way most of the times then this should be the default way. In this cases, explicit configuration would be needed only in those cases where you can&#8217;t follow the conventions for any reason.</p>
135
+ <h4 id="naming-conventions">2.1 Naming Conventions</h4>
136
+ <p>By default, Active Record uses some naming conventions to find out how the mapping between models and database tables should be created. Rails will pluralize your class names to find the respective database table. So, for a class <tt>Book</tt>, you should have a database table called <strong>books</strong>. The Rails pluralization mechanisms are very powerful, being capable to pluralize (and singularize) both regular and irregular words. When using class names composed of two or more words, the model class name should follow the Ruby conventions, using the CamelCase form, while the table name must contain the words separated by underscores. Examples:</p>
137
+ <ul>
138
+ <li>Database Table &#8211; Plural with underscores separating words (e.g., <tt>book_clubs</tt>)</li>
139
+ <li>Model Class &#8211; Singular with the first letter of each word capitalized (e.g., <tt>BookClub</tt>)</li>
140
+ </ul>
141
+ <table>
142
+ <tr>
143
+ <th>Model / Class </th>
144
+ <th>Table / Schema </th>
145
+ </tr>
146
+ <tr>
147
+ <td><tt>Post</tt> </td>
148
+ <td><tt>posts</tt></td>
149
+ </tr>
150
+ <tr>
151
+ <td><tt>LineItem</tt> </td>
152
+ <td><tt>line_items</tt></td>
153
+ </tr>
154
+ <tr>
155
+ <td><tt>Deer</tt> </td>
156
+ <td><tt>deer</tt></td>
157
+ </tr>
158
+ <tr>
159
+ <td><tt>Mouse</tt> </td>
160
+ <td><tt>mice</tt></td>
161
+ </tr>
162
+ <tr>
163
+ <td><tt>Person</tt> </td>
164
+ <td><tt>people</tt></td>
165
+ </tr>
166
+ </table>
167
+ <h4 id="schema-conventions">2.2 Schema Conventions</h4>
168
+ <p>Active Record uses naming conventions for the columns in database tables, depending on the purpose of these columns.</p>
169
+ <ul>
170
+ <li><strong>Foreign keys</strong> &#8211; These fields should be named following the pattern <tt>singularized_table_name_id</tt> (e.g., <tt>item_id</tt>, <tt>order_id</tt>). These are the fields that Active Record will look for when you create associations between your models.</li>
171
+ <li><strong>Primary keys</strong> &#8211; By default, Active Record will use an integer column named <tt>id</tt> as the table&#8217;s primary key. When using <a href="migrations.html">Rails Migrations</a> to create your tables, this column will be automatically created.</li>
172
+ </ul>
173
+ <p>There are also some optional column names that will create additional features to Active Record instances:</p>
174
+ <ul>
175
+ <li><tt>created_at</tt> &#8211; Automatically gets set to the current date and time when the record is first created.</li>
176
+ <li><tt>created_on</tt> &#8211; Automatically gets set to the current date when the record is first created.</li>
177
+ <li><tt>updated_at</tt> &#8211; Automatically gets set to the current date and time whenever the record is updated.</li>
178
+ <li><tt>updated_on</tt> &#8211; Automatically gets set to the current date whenever the record is updated.</li>
179
+ <li><tt>lock_version</tt> &#8211; Adds <a href="http://api.rubyonrails.org/classes/ActiveRecord/Locking.html">optimistic locking</a> to a model.</li>
180
+ <li><tt>type</tt> &#8211; Specifies that the model uses <a href="http://api.rubyonrails.org/classes/ActiveRecord/Base.html">Single Table Inheritance</a></li>
181
+ <li><tt>(table_name)_count</tt> &#8211; Used to cache the number of belonging objects on associations. For example, a <tt>comments_count</tt> column in a <tt>Post</tt> class that has many instances of <tt>Comment</tt> will cache the number of existent comments for each post.</li>
182
+ </ul>
183
+ <div class="note"><p>While these column names are optional, they are in fact reserved by Active Record. Steer clear of reserved keywords unless you want the extra functionality. For example, <tt>type</tt> is a reserved keyword used to designate a table using Single Table Inheritance (<span class="caps">STI</span>). If you are not using <span class="caps">STI</span>, try an analogous keyword like &#8220;context&#8221;, that may still accurately describe the data you are modeling.</p></div>
184
+ <h3 id="creating-active-record-models">3 Creating Active Record Models</h3>
185
+ <p>It is very easy to create Active Record models. All you have to do is to subclass the <tt>ActiveRecord::Base</tt> class and you&#8217;re good to go:</p>
186
+ <div class="code_container">
187
+ <pre class="brush: ruby; gutter: false; toolbar: false">
188
+ class Product &lt; ActiveRecord::Base
189
+ end
190
+ </pre>
191
+ </div>
192
+ <p>This will create a <tt>Product</tt> model, mapped to a <tt>products</tt> table at the database. By doing this you&#8217;ll also have the ability to map the columns of each row in that table with the attributes of the instances of your model. Suppose that the <tt>products</tt> table was created using an <span class="caps">SQL</span> sentence like:</p>
193
+ <div class="code_container">
194
+ <pre class="brush: sql; gutter: false; toolbar: false">
195
+ CREATE TABLE products (
196
+ id int(11) NOT NULL auto_increment,
197
+ name varchar(255),
198
+ PRIMARY KEY (id)
199
+ );
200
+ </pre>
201
+ </div>
202
+ <p>Following the table schema above, you would be able to write code like the following:</p>
203
+ <div class="code_container">
204
+ <pre class="brush: ruby; gutter: false; toolbar: false">
205
+ p = Product.new
206
+ p.name = &quot;Some Book&quot;
207
+ puts p.name # &quot;Some Book&quot;
208
+ </pre>
209
+ </div>
210
+ <h3 id="overriding-the-naming-conventions">4 Overriding the Naming Conventions</h3>
211
+ <p>What if you need to follow a different naming convention or need to use your Rails application with a legacy database? No problem, you can easily override the default conventions.</p>
212
+ <p>You can use the <tt>ActiveRecord::Base.set_table_name</tt> method to specify the table name that should be used:</p>
213
+ <div class="code_container">
214
+ <pre class="brush: ruby; gutter: false; toolbar: false">
215
+ class Product &lt; ActiveRecord::Base
216
+ set_table_name &quot;PRODUCT&quot;
217
+ end
218
+ </pre>
219
+ </div>
220
+ <p>If you do so, you will have to define manually the class name that is hosting the fixtures (class_name.yml) using the <tt>set_fixture_class</tt> method in your test definition:</p>
221
+ <div class="code_container">
222
+ <pre class="brush: ruby; gutter: false; toolbar: false">
223
+ class FunnyJoke &lt; ActiveSupport::TestCase
224
+ set_fixture_class :funny_jokes =&gt; 'Joke'
225
+ fixtures :funny_jokes
226
+ ...
227
+ end
228
+ </pre>
229
+ </div>
230
+ <p>It&#8217;s also possible to override the column that should be used as the table&#8217;s primary key using the <tt>ActiveRecord::Base.set_primary_key</tt> method:</p>
231
+ <div class="code_container">
232
+ <pre class="brush: ruby; gutter: false; toolbar: false">
233
+ class Product &lt; ActiveRecord::Base
234
+ set_primary_key &quot;product_id&quot;
235
+ end
236
+ </pre>
237
+ </div>
238
+ <h3 id="crud-reading-and-writing-data">5 <span class="caps">CRUD</span>: Reading and Writing Data</h3>
239
+ <p><span class="caps">CRUD</span> is an acronym for the four verbs we use to operate on data: *C*reate, *R*ead, *U*pdate and *D*elete. Active Record automatically creates methods to allow an application to read and manipulate data stored within its tables.</p>
240
+ <h4 id="create">5.1 Create</h4>
241
+ <p>Active Record objects can be created from a hash, a block or have their attributes manually set after creation. The <tt>new</tt> method will return a new object while <tt>create</tt> will return the object and save it to the database.</p>
242
+ <p>For example, given a model <tt>User</tt> with attributes of <tt>name</tt> and <tt>occupation</tt>, the <tt>create</tt> method call will create and save a new record into the database:</p>
243
+ <div class="code_container">
244
+ <pre class="brush: ruby; gutter: false; toolbar: false">
245
+ user = User.create(:name =&gt; &quot;David&quot;, :occupation =&gt; &quot;Code Artist&quot;)
246
+ </pre>
247
+ </div>
248
+ <p>Using the <tt>new</tt> method, an object can be created without being saved:</p>
249
+ <div class="code_container">
250
+ <pre class="brush: ruby; gutter: false; toolbar: false">
251
+ user = User.new
252
+ user.name = &quot;David&quot;
253
+ user.occupation = &quot;Code Artist&quot;
254
+ </pre>
255
+ </div>
256
+ <p>A call to <tt>user.save</tt> will commit the record to the database.</p>
257
+ <p>Finally, if a block is provided, both <tt>create</tt> and <tt>new</tt> will yield the new object to that block for initialization:</p>
258
+ <div class="code_container">
259
+ <pre class="brush: ruby; gutter: false; toolbar: false">
260
+ user = User.new do |u|
261
+ u.name = &quot;David&quot;
262
+ u.occupation = &quot;Code Artist&quot;
263
+ end
264
+ </pre>
265
+ </div>
266
+ <h4 id="read">5.2 Read</h4>
267
+ <p>Active Record provides a rich <span class="caps">API</span> for accessing data within a database. Below are a few examples of different data access methods provided by Active Record.</p>
268
+ <div class="code_container">
269
+ <pre class="brush: ruby; gutter: false; toolbar: false">
270
+ # return array with all records
271
+ users = User.all
272
+ </pre>
273
+ </div>
274
+ <div class="code_container">
275
+ <pre class="brush: ruby; gutter: false; toolbar: false">
276
+ # return the first record
277
+ user = User.first
278
+ </pre>
279
+ </div>
280
+ <div class="code_container">
281
+ <pre class="brush: ruby; gutter: false; toolbar: false">
282
+ # return the first user named David
283
+ david = User.find_by_name('David')
284
+ </pre>
285
+ </div>
286
+ <div class="code_container">
287
+ <pre class="brush: ruby; gutter: false; toolbar: false">
288
+ # find all users named David who are Code Artists and sort by created_at in reverse chronological order
289
+ users = User.where(:name =&gt; 'David', :occupation =&gt; 'Code Artist').order('created_at DESC')
290
+ </pre>
291
+ </div>
292
+ <p>You can learn more about querying an Active Record model in the &#8220;Active Record Query Interface&#8221;:&#8220;active_record_querying.html&#8221; guide.</p>
293
+ <h4 id="update">5.3 Update</h4>
294
+ <p>Once an Active Record object has been retrieved, its attributes can be modified and it can be saved to the database.</p>
295
+ <div class="code_container">
296
+ <pre class="brush: ruby; gutter: false; toolbar: false">
297
+ user = User.find_by_name('David')
298
+ user.name = 'Dave'
299
+ user.save
300
+ </pre>
301
+ </div>
302
+ <h4 id="delete">5.4 Delete</h4>
303
+ <p>Likewise, once retrieved an Active Record object can be destroyed which removes it from the database.</p>
304
+ <div class="code_container">
305
+ <pre class="brush: ruby; gutter: false; toolbar: false">
306
+ user = User.find_by_name('David')
307
+ user.destroy
308
+ </pre>
309
+ </div>
310
+ <h3 id="validations">6 Validations</h3>
311
+ <p>Active Record allows you to validate the state of a model before it gets written into the database. There are several methods that you can use to check your models and validate that an attribute value is not empty, is unique and not already in the database, follows a specific format and many more. You can learn more about validations in the <a href="active_record_validations_callbacks.html#validations-overview">Active Record Validations and Callbacks guide</a>.</p>
312
+ <h3 id="callbacks">7 Callbacks</h3>
313
+ <p>Active Record callbacks allow you to attach code to certain events in the life-cycle of your models. This enables you to add behavior to your models by transparently executing code when those events occur, like when you create a new record, update it, destroy it and so on. You can learn more about callbacks in the <a href="active_record_validations_callbacks.html#callbacks-overview">Active Record Validations and Callbacks guide</a>.</p>
314
+ <h3 id="migrations">8 Migrations</h3>
315
+ <p>Rails provides a domain-specific language for managing a database schema called migrations. Migrations are stored in files which are executed against any database that Active Record support using rake. Rails keeps track of which files have been committed to the database and provides rollback features. You can learn more about migrations in the <a href="migrations.html">Active Record Migrations guide</a></p>
316
+
317
+ <h3>Feedback</h3>
318
+ <p>
319
+ You're encouraged to help improve the quality of this guide.
320
+ </p>
321
+ <p>
322
+ If you see any typos or factual errors you are confident to
323
+ patch, please clone <a href="https://github.com/lifo/docrails">docrails</a>
324
+ and push the change yourself. That branch of Rails has public write access.
325
+ Commits are still reviewed, but that happens after you've submitted your
326
+ contribution. <a href="https://github.com/lifo/docrails">docrails</a> is
327
+ cross-merged with master periodically.
328
+ </p>
329
+ <p>
330
+ You may also find incomplete content, or stuff that is not up to date.
331
+ Please do add any missing documentation for master. Check the
332
+ <a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a>
333
+ for style and conventions.
334
+ </p>
335
+ <p>
336
+ If for whatever reason you spot something to fix but cannot patch it yourself, please
337
+ <a href="https://github.com/rails/rails/issues">open an issue</a>.
338
+ </p>
339
+ <p>And last but not least, any kind of discussion regarding Ruby on Rails
340
+ documentation is very welcome in the <a href="http://groups.google.com/group/rubyonrails-docs">rubyonrails-docs mailing list</a>.
341
+ </p>
342
+ </div>
343
+ </div>
344
+ </div>
345
+
346
+ <hr class="hide" />
347
+ <div id="footer">
348
+ <div class="wrapper">
349
+ <p>This work is licensed under a <a href="http://creativecommons.org/licenses/by-sa/3.0/">Creative Commons Attribution-Share Alike 3.0</a> License</p>
350
+ <p>"Rails", "Ruby on Rails", and the Rails logo are trademarks of David Heinemeier Hansson. All rights reserved.</p>
351
+ </div>
352
+ </div>
353
+
354
+ <script type="text/javascript" src="javascripts/guides.js"></script>
355
+ <script type="text/javascript" src="javascripts/syntaxhighlighter/shCore.js"></script>
356
+ <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushRuby.js"></script>
357
+ <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushXml.js"></script>
358
+ <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushSql.js"></script>
359
+ <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushPlain.js"></script>
360
+ <script type="text/javascript">
361
+ SyntaxHighlighter.all()
362
+ </script>
363
+ </body>
364
+ </html>