rails 4.2.0.beta4 → 4.2.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (230) hide show
  1. checksums.yaml +4 -4
  2. data/guides/Rakefile +3 -1
  3. data/guides/bug_report_templates/action_controller_gem.rb +2 -2
  4. data/guides/bug_report_templates/action_controller_master.rb +2 -2
  5. data/guides/source/3_0_release_notes.md +2 -2
  6. data/guides/source/4_1_release_notes.md +1 -1
  7. data/guides/source/4_2_release_notes.md +250 -225
  8. data/guides/source/_welcome.html.erb +1 -1
  9. data/guides/source/action_controller_overview.md +6 -6
  10. data/guides/source/action_mailer_basics.md +26 -11
  11. data/guides/source/action_view_overview.md +0 -1
  12. data/guides/source/active_job_basics.md +29 -23
  13. data/guides/source/active_record_postgresql.md +2 -7
  14. data/guides/source/active_record_validations.md +1 -0
  15. data/guides/source/active_support_core_extensions.md +1 -1
  16. data/guides/source/active_support_instrumentation.md +5 -5
  17. data/guides/source/asset_pipeline.md +2 -2
  18. data/guides/source/association_basics.md +0 -2
  19. data/guides/source/command_line.md +6 -15
  20. data/guides/source/configuring.md +4 -4
  21. data/guides/source/documents.yaml +0 -1
  22. data/guides/source/engines.md +26 -15
  23. data/guides/source/form_helpers.md +10 -2
  24. data/guides/source/getting_started.md +9 -9
  25. data/guides/source/rails_on_rack.md +2 -2
  26. data/guides/source/ruby_on_rails_guides_guidelines.md +1 -0
  27. data/guides/source/security.md +1 -1
  28. data/guides/source/testing.md +80 -13
  29. data/guides/source/upgrading_ruby_on_rails.md +23 -20
  30. metadata +29 -229
  31. data/guides/output/2_2_release_notes.html +0 -724
  32. data/guides/output/2_3_release_notes.html +0 -870
  33. data/guides/output/3_0_release_notes.html +0 -773
  34. data/guides/output/3_1_release_notes.html +0 -740
  35. data/guides/output/3_2_release_notes.html +0 -797
  36. data/guides/output/4_0_release_notes.html +0 -523
  37. data/guides/output/4_1_release_notes.html +0 -806
  38. data/guides/output/4_2_release_notes.html +0 -728
  39. data/guides/output/Gemfile +0 -6
  40. data/guides/output/_license.html +0 -226
  41. data/guides/output/_welcome.html +0 -233
  42. data/guides/output/action_controller_overview.html +0 -1335
  43. data/guides/output/action_mailer_basics.html +0 -928
  44. data/guides/output/action_view_overview.html +0 -1509
  45. data/guides/output/active_job_basics.html +0 -546
  46. data/guides/output/active_model_basics.html +0 -438
  47. data/guides/output/active_record_basics.html +0 -594
  48. data/guides/output/active_record_callbacks.html +0 -592
  49. data/guides/output/active_record_migrations.html +0 -1123
  50. data/guides/output/active_record_postgresql.html +0 -675
  51. data/guides/output/active_record_querying.html +0 -1796
  52. data/guides/output/active_record_validations.html +0 -1301
  53. data/guides/output/active_support_core_extensions.html +0 -3450
  54. data/guides/output/active_support_instrumentation.html +0 -1121
  55. data/guides/output/api_documentation_guidelines.html +0 -498
  56. data/guides/output/asset_pipeline.html +0 -1167
  57. data/guides/output/association_basics.html +0 -2086
  58. data/guides/output/caching_with_rails.html +0 -553
  59. data/guides/output/command_line.html +0 -791
  60. data/guides/output/configuring.html +0 -1055
  61. data/guides/output/contributing_to_ruby_on_rails.html +0 -657
  62. data/guides/output/credits.html +0 -284
  63. data/guides/output/debugging_rails_applications.html +0 -1014
  64. data/guides/output/development_dependencies_install.html +0 -478
  65. data/guides/output/engines.html +0 -1438
  66. data/guides/output/form_helpers.html +0 -1074
  67. data/guides/output/generators.html +0 -838
  68. data/guides/output/getting_started.html +0 -2092
  69. data/guides/output/i18n.html +0 -1198
  70. data/guides/output/images/akshaysurve.jpg +0 -0
  71. data/guides/output/images/belongs_to.png +0 -0
  72. data/guides/output/images/book_icon.gif +0 -0
  73. data/guides/output/images/bullet.gif +0 -0
  74. data/guides/output/images/chapters_icon.gif +0 -0
  75. data/guides/output/images/check_bullet.gif +0 -0
  76. data/guides/output/images/credits_pic_blank.gif +0 -0
  77. data/guides/output/images/csrf.png +0 -0
  78. data/guides/output/images/edge_badge.png +0 -0
  79. data/guides/output/images/favicon.ico +0 -0
  80. data/guides/output/images/feature_tile.gif +0 -0
  81. data/guides/output/images/footer_tile.gif +0 -0
  82. data/guides/output/images/fxn.png +0 -0
  83. data/guides/output/images/getting_started/article_with_comments.png +0 -0
  84. data/guides/output/images/getting_started/challenge.png +0 -0
  85. data/guides/output/images/getting_started/confirm_dialog.png +0 -0
  86. data/guides/output/images/getting_started/forbidden_attributes_for_new_article.png +0 -0
  87. data/guides/output/images/getting_started/forbidden_attributes_for_new_post.png +0 -0
  88. data/guides/output/images/getting_started/form_with_errors.png +0 -0
  89. data/guides/output/images/getting_started/index_action_with_edit_link.png +0 -0
  90. data/guides/output/images/getting_started/new_article.png +0 -0
  91. data/guides/output/images/getting_started/new_post.png +0 -0
  92. data/guides/output/images/getting_started/post_with_comments.png +0 -0
  93. data/guides/output/images/getting_started/rails_welcome.png +0 -0
  94. data/guides/output/images/getting_started/routing_error_no_controller.png +0 -0
  95. data/guides/output/images/getting_started/routing_error_no_route_matches.png +0 -0
  96. data/guides/output/images/getting_started/show_action_for_articles.png +0 -0
  97. data/guides/output/images/getting_started/show_action_for_posts.png +0 -0
  98. data/guides/output/images/getting_started/template_is_missing_articles_new.png +0 -0
  99. data/guides/output/images/getting_started/template_is_missing_posts_new.png +0 -0
  100. data/guides/output/images/getting_started/undefined_method_post_path.png +0 -0
  101. data/guides/output/images/getting_started/unknown_action_create_for_articles.png +0 -0
  102. data/guides/output/images/getting_started/unknown_action_create_for_posts.png +0 -0
  103. data/guides/output/images/getting_started/unknown_action_new_for_articles.png +0 -0
  104. data/guides/output/images/getting_started/unknown_action_new_for_posts.png +0 -0
  105. data/guides/output/images/grey_bullet.gif +0 -0
  106. data/guides/output/images/habtm.png +0 -0
  107. data/guides/output/images/has_many.png +0 -0
  108. data/guides/output/images/has_many_through.png +0 -0
  109. data/guides/output/images/has_one.png +0 -0
  110. data/guides/output/images/has_one_through.png +0 -0
  111. data/guides/output/images/header_backdrop.png +0 -0
  112. data/guides/output/images/header_tile.gif +0 -0
  113. data/guides/output/images/i18n/demo_html_safe.png +0 -0
  114. data/guides/output/images/i18n/demo_localized_pirate.png +0 -0
  115. data/guides/output/images/i18n/demo_translated_en.png +0 -0
  116. data/guides/output/images/i18n/demo_translated_pirate.png +0 -0
  117. data/guides/output/images/i18n/demo_translation_missing.png +0 -0
  118. data/guides/output/images/i18n/demo_untranslated.png +0 -0
  119. data/guides/output/images/icons/README +0 -5
  120. data/guides/output/images/icons/callouts/1.png +0 -0
  121. data/guides/output/images/icons/callouts/10.png +0 -0
  122. data/guides/output/images/icons/callouts/11.png +0 -0
  123. data/guides/output/images/icons/callouts/12.png +0 -0
  124. data/guides/output/images/icons/callouts/13.png +0 -0
  125. data/guides/output/images/icons/callouts/14.png +0 -0
  126. data/guides/output/images/icons/callouts/15.png +0 -0
  127. data/guides/output/images/icons/callouts/2.png +0 -0
  128. data/guides/output/images/icons/callouts/3.png +0 -0
  129. data/guides/output/images/icons/callouts/4.png +0 -0
  130. data/guides/output/images/icons/callouts/5.png +0 -0
  131. data/guides/output/images/icons/callouts/6.png +0 -0
  132. data/guides/output/images/icons/callouts/7.png +0 -0
  133. data/guides/output/images/icons/callouts/8.png +0 -0
  134. data/guides/output/images/icons/callouts/9.png +0 -0
  135. data/guides/output/images/icons/caution.png +0 -0
  136. data/guides/output/images/icons/example.png +0 -0
  137. data/guides/output/images/icons/home.png +0 -0
  138. data/guides/output/images/icons/important.png +0 -0
  139. data/guides/output/images/icons/next.png +0 -0
  140. data/guides/output/images/icons/note.png +0 -0
  141. data/guides/output/images/icons/prev.png +0 -0
  142. data/guides/output/images/icons/tip.png +0 -0
  143. data/guides/output/images/icons/up.png +0 -0
  144. data/guides/output/images/icons/warning.png +0 -0
  145. data/guides/output/images/nav_arrow.gif +0 -0
  146. data/guides/output/images/oscardelben.jpg +0 -0
  147. data/guides/output/images/polymorphic.png +0 -0
  148. data/guides/output/images/radar.png +0 -0
  149. data/guides/output/images/rails4_features.png +0 -0
  150. data/guides/output/images/rails_guides_kindle_cover.jpg +0 -0
  151. data/guides/output/images/rails_guides_logo.gif +0 -0
  152. data/guides/output/images/rails_logo_remix.gif +0 -0
  153. data/guides/output/images/session_fixation.png +0 -0
  154. data/guides/output/images/tab_grey.gif +0 -0
  155. data/guides/output/images/tab_info.gif +0 -0
  156. data/guides/output/images/tab_note.gif +0 -0
  157. data/guides/output/images/tab_red.gif +0 -0
  158. data/guides/output/images/tab_yellow.gif +0 -0
  159. data/guides/output/images/tab_yellow.png +0 -0
  160. data/guides/output/images/vijaydev.jpg +0 -0
  161. data/guides/output/index.html +0 -354
  162. data/guides/output/initialization.html +0 -876
  163. data/guides/output/javascripts/guides.js +0 -59
  164. data/guides/output/javascripts/jquery.min.js +0 -4
  165. data/guides/output/javascripts/responsive-tables.js +0 -43
  166. data/guides/output/javascripts/syntaxhighlighter/shBrushAS3.js +0 -59
  167. data/guides/output/javascripts/syntaxhighlighter/shBrushAppleScript.js +0 -75
  168. data/guides/output/javascripts/syntaxhighlighter/shBrushBash.js +0 -59
  169. data/guides/output/javascripts/syntaxhighlighter/shBrushCSharp.js +0 -65
  170. data/guides/output/javascripts/syntaxhighlighter/shBrushColdFusion.js +0 -100
  171. data/guides/output/javascripts/syntaxhighlighter/shBrushCpp.js +0 -97
  172. data/guides/output/javascripts/syntaxhighlighter/shBrushCss.js +0 -91
  173. data/guides/output/javascripts/syntaxhighlighter/shBrushDelphi.js +0 -55
  174. data/guides/output/javascripts/syntaxhighlighter/shBrushDiff.js +0 -41
  175. data/guides/output/javascripts/syntaxhighlighter/shBrushErlang.js +0 -52
  176. data/guides/output/javascripts/syntaxhighlighter/shBrushGroovy.js +0 -67
  177. data/guides/output/javascripts/syntaxhighlighter/shBrushJScript.js +0 -52
  178. data/guides/output/javascripts/syntaxhighlighter/shBrushJava.js +0 -57
  179. data/guides/output/javascripts/syntaxhighlighter/shBrushJavaFX.js +0 -58
  180. data/guides/output/javascripts/syntaxhighlighter/shBrushPerl.js +0 -72
  181. data/guides/output/javascripts/syntaxhighlighter/shBrushPhp.js +0 -88
  182. data/guides/output/javascripts/syntaxhighlighter/shBrushPlain.js +0 -33
  183. data/guides/output/javascripts/syntaxhighlighter/shBrushPowerShell.js +0 -74
  184. data/guides/output/javascripts/syntaxhighlighter/shBrushPython.js +0 -64
  185. data/guides/output/javascripts/syntaxhighlighter/shBrushRuby.js +0 -55
  186. data/guides/output/javascripts/syntaxhighlighter/shBrushSass.js +0 -94
  187. data/guides/output/javascripts/syntaxhighlighter/shBrushScala.js +0 -51
  188. data/guides/output/javascripts/syntaxhighlighter/shBrushSql.js +0 -66
  189. data/guides/output/javascripts/syntaxhighlighter/shBrushVb.js +0 -56
  190. data/guides/output/javascripts/syntaxhighlighter/shBrushXml.js +0 -69
  191. data/guides/output/javascripts/syntaxhighlighter/shCore.js +0 -17
  192. data/guides/output/layout.html +0 -448
  193. data/guides/output/layouts_and_rendering.html +0 -1541
  194. data/guides/output/maintenance_policy.html +0 -257
  195. data/guides/output/migrations.html +0 -1360
  196. data/guides/output/nested_model_forms.html +0 -412
  197. data/guides/output/plugins.html +0 -644
  198. data/guides/output/rails_application_templates.html +0 -450
  199. data/guides/output/rails_on_rack.html +0 -547
  200. data/guides/output/routing.html +0 -1631
  201. data/guides/output/ruby_on_rails_guides_guidelines.html +0 -329
  202. data/guides/output/security.html +0 -935
  203. data/guides/output/stylesheets/fixes.css +0 -16
  204. data/guides/output/stylesheets/kindle.css +0 -11
  205. data/guides/output/stylesheets/main.css +0 -713
  206. data/guides/output/stylesheets/print.css +0 -52
  207. data/guides/output/stylesheets/reset.css +0 -43
  208. data/guides/output/stylesheets/responsive-tables.css +0 -50
  209. data/guides/output/stylesheets/style.css +0 -13
  210. data/guides/output/stylesheets/syntaxhighlighter/shCore.css +0 -226
  211. data/guides/output/stylesheets/syntaxhighlighter/shCoreDefault.css +0 -328
  212. data/guides/output/stylesheets/syntaxhighlighter/shCoreDjango.css +0 -331
  213. data/guides/output/stylesheets/syntaxhighlighter/shCoreEclipse.css +0 -339
  214. data/guides/output/stylesheets/syntaxhighlighter/shCoreEmacs.css +0 -324
  215. data/guides/output/stylesheets/syntaxhighlighter/shCoreFadeToGrey.css +0 -328
  216. data/guides/output/stylesheets/syntaxhighlighter/shCoreMDUltra.css +0 -324
  217. data/guides/output/stylesheets/syntaxhighlighter/shCoreMidnight.css +0 -324
  218. data/guides/output/stylesheets/syntaxhighlighter/shCoreRDark.css +0 -324
  219. data/guides/output/stylesheets/syntaxhighlighter/shThemeDefault.css +0 -117
  220. data/guides/output/stylesheets/syntaxhighlighter/shThemeDjango.css +0 -120
  221. data/guides/output/stylesheets/syntaxhighlighter/shThemeEclipse.css +0 -128
  222. data/guides/output/stylesheets/syntaxhighlighter/shThemeEmacs.css +0 -113
  223. data/guides/output/stylesheets/syntaxhighlighter/shThemeFadeToGrey.css +0 -117
  224. data/guides/output/stylesheets/syntaxhighlighter/shThemeMDUltra.css +0 -113
  225. data/guides/output/stylesheets/syntaxhighlighter/shThemeMidnight.css +0 -113
  226. data/guides/output/stylesheets/syntaxhighlighter/shThemeRDark.css +0 -113
  227. data/guides/output/stylesheets/syntaxhighlighter/shThemeRailsGuides.css +0 -116
  228. data/guides/output/testing.html +0 -1350
  229. data/guides/output/upgrading_ruby_on_rails.html +0 -1135
  230. data/guides/output/working_with_javascript_in_rails.html +0 -587
@@ -1,438 +0,0 @@
1
- <!DOCTYPE html>
2
-
3
- <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
4
- <head>
5
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
6
- <meta name="viewport" content="width=device-width, initial-scale=1"/>
7
-
8
- <title>Active Model Basics — Ruby on Rails Guides</title>
9
- <link rel="stylesheet" type="text/css" href="stylesheets/style.css" />
10
- <link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print" />
11
-
12
- <link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shCore.css" />
13
- <link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shThemeRailsGuides.css" />
14
-
15
- <link rel="stylesheet" type="text/css" href="stylesheets/fixes.css" />
16
-
17
- <link href="images/favicon.ico" rel="shortcut icon" type="image/x-icon" />
18
- </head>
19
- <body class="guide">
20
- <div id="topNav">
21
- <div class="wrapper">
22
- <strong class="more-info-label">More at <a href="http://rubyonrails.org/">rubyonrails.org:</a> </strong>
23
- <span class="red-button more-info-button">
24
- More Ruby on Rails
25
- </span>
26
- <ul class="more-info-links s-hidden">
27
- <li class="more-info"><a href="http://rubyonrails.org/">Overview</a></li>
28
- <li class="more-info"><a href="http://rubyonrails.org/download">Download</a></li>
29
- <li class="more-info"><a href="http://rubyonrails.org/deploy">Deploy</a></li>
30
- <li class="more-info"><a href="https://github.com/rails/rails">Code</a></li>
31
- <li class="more-info"><a href="http://rubyonrails.org/screencasts">Screencasts</a></li>
32
- <li class="more-info"><a href="http://rubyonrails.org/documentation">Documentation</a></li>
33
- <li class="more-info"><a href="http://rubyonrails.org/community">Community</a></li>
34
- <li class="more-info"><a href="http://weblog.rubyonrails.org/">Blog</a></li>
35
- </ul>
36
- </div>
37
- </div>
38
- <div id="header">
39
- <div class="wrapper clearfix">
40
- <h1><a href="index.html" title="Return to home page">Guides.rubyonrails.org</a></h1>
41
- <ul class="nav">
42
- <li><a class="nav-item" href="index.html">Home</a></li>
43
- <li class="guides-index guides-index-large">
44
- <a href="index.html" id="guidesMenu" class="guides-index-item nav-item">Guides Index</a>
45
- <div id="guides" class="clearfix" style="display: none;">
46
- <hr />
47
- <dl class="L">
48
- <dt>Start Here</dt>
49
- <dd><a href="getting_started.html">Getting Started with Rails</a></dd>
50
- <dt>Models</dt>
51
- <dd><a href="active_record_basics.html">Active Record Basics</a></dd>
52
- <dd><a href="active_record_migrations.html">Active Record Migrations</a></dd>
53
- <dd><a href="active_record_validations.html">Active Record Validations</a></dd>
54
- <dd><a href="active_record_callbacks.html">Active Record Callbacks</a></dd>
55
- <dd><a href="association_basics.html">Active Record Associations</a></dd>
56
- <dd><a href="active_record_querying.html">Active Record Query Interface</a></dd>
57
- <dt>Views</dt>
58
- <dd><a href="layouts_and_rendering.html">Layouts and Rendering in Rails</a></dd>
59
- <dd><a href="form_helpers.html">Action View Form Helpers</a></dd>
60
- <dt>Controllers</dt>
61
- <dd><a href="action_controller_overview.html">Action Controller Overview</a></dd>
62
- <dd><a href="routing.html">Rails Routing from the Outside In</a></dd>
63
- </dl>
64
- <dl class="R">
65
- <dt>Digging Deeper</dt>
66
- <dd><a href="active_support_core_extensions.html">Active Support Core Extensions</a></dd>
67
- <dd><a href="i18n.html">Rails Internationalization API</a></dd>
68
- <dd><a href="action_mailer_basics.html">Action Mailer Basics</a></dd>
69
- <dd><a href="active_job_basics.html">Active Job Basics</a></dd>
70
- <dd><a href="security.html">Securing Rails Applications</a></dd>
71
- <dd><a href="debugging_rails_applications.html">Debugging Rails Applications</a></dd>
72
- <dd><a href="configuring.html">Configuring Rails Applications</a></dd>
73
- <dd><a href="command_line.html">Rails Command Line Tools and Rake Tasks</a></dd>
74
- <dd><a href="asset_pipeline.html">Asset Pipeline</a></dd>
75
- <dd><a href="working_with_javascript_in_rails.html">Working with JavaScript in Rails</a></dd>
76
- <dt>Extending Rails</dt>
77
- <dd><a href="rails_on_rack.html">Rails on Rack</a></dd>
78
- <dd><a href="generators.html">Creating and Customizing Rails Generators</a></dd>
79
- <dt>Contributing to Ruby on Rails</dt>
80
- <dd><a href="contributing_to_ruby_on_rails.html">Contributing to Ruby on Rails</a></dd>
81
- <dd><a href="api_documentation_guidelines.html">API Documentation Guidelines</a></dd>
82
- <dd><a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a></dd>
83
- <dt>Maintenance Policy</dt>
84
- <dd><a href="maintenance_policy.html">Maintenance Policy</a></dd>
85
- <dt>Release Notes</dt>
86
- <dd><a href="upgrading_ruby_on_rails.html">Upgrading Ruby on Rails</a></dd>
87
- <dd><a href="4_1_release_notes.html">Ruby on Rails 4.1 Release Notes</a></dd>
88
- <dd><a href="4_0_release_notes.html">Ruby on Rails 4.0 Release Notes</a></dd>
89
- <dd><a href="3_2_release_notes.html">Ruby on Rails 3.2 Release Notes</a></dd>
90
- <dd><a href="3_1_release_notes.html">Ruby on Rails 3.1 Release Notes</a></dd>
91
- <dd><a href="3_0_release_notes.html">Ruby on Rails 3.0 Release Notes</a></dd>
92
- <dd><a href="2_3_release_notes.html">Ruby on Rails 2.3 Release Notes</a></dd>
93
- <dd><a href="2_2_release_notes.html">Ruby on Rails 2.2 Release Notes</a></dd>
94
- </dl>
95
- </div>
96
- </li>
97
- <li><a class="nav-item" href="contributing_to_ruby_on_rails.html">Contribute</a></li>
98
- <li><a class="nav-item" href="credits.html">Credits</a></li>
99
- <li class="guides-index guides-index-small">
100
- <select class="guides-index-item nav-item">
101
- <option value="index.html">Guides Index</option>
102
- <optgroup label="Start Here">
103
- <option value="getting_started.html">Getting Started with Rails</option>
104
- </optgroup>
105
- <optgroup label="Models">
106
- <option value="active_record_basics.html">Active Record Basics</option>
107
- <option value="active_record_migrations.html">Active Record Migrations</option>
108
- <option value="active_record_validations.html">Active Record Validations</option>
109
- <option value="active_record_callbacks.html">Active Record Callbacks</option>
110
- <option value="association_basics.html">Active Record Associations</option>
111
- <option value="active_record_querying.html">Active Record Query Interface</option>
112
- </optgroup>
113
- <optgroup label="Views">
114
- <option value="layouts_and_rendering.html">Layouts and Rendering in Rails</option>
115
- <option value="form_helpers.html">Action View Form Helpers</option>
116
- </optgroup>
117
- <optgroup label="Controllers">
118
- <option value="action_controller_overview.html">Action Controller Overview</option>
119
- <option value="routing.html">Rails Routing from the Outside In</option>
120
- </optgroup>
121
- <optgroup label="Digging Deeper">
122
- <option value="active_support_core_extensions.html">Active Support Core Extensions</option>
123
- <option value="i18n.html">Rails Internationalization API</option>
124
- <option value="action_mailer_basics.html">Action Mailer Basics</option>
125
- <option value="active_job_basics.html">Active Job Basics</option>
126
- <option value="security.html">Securing Rails Applications</option>
127
- <option value="debugging_rails_applications.html">Debugging Rails Applications</option>
128
- <option value="configuring.html">Configuring Rails Applications</option>
129
- <option value="command_line.html">Rails Command Line Tools and Rake Tasks</option>
130
- <option value="asset_pipeline.html">Asset Pipeline</option>
131
- <option value="working_with_javascript_in_rails.html">Working with JavaScript in Rails</option>
132
- </optgroup>
133
- <optgroup label="Extending Rails">
134
- <option value="rails_on_rack.html">Rails on Rack</option>
135
- <option value="generators.html">Creating and Customizing Rails Generators</option>
136
- </optgroup>
137
- <optgroup label="Contributing to Ruby on Rails">
138
- <option value="contributing_to_ruby_on_rails.html">Contributing to Ruby on Rails</option>
139
- <option value="api_documentation_guidelines.html">API Documentation Guidelines</option>
140
- <option value="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</option>
141
- </optgroup>
142
- <optgroup label="Maintenance Policy">
143
- <option value="maintenance_policy.html">Maintenance Policy</option>
144
- </optgroup>
145
- <optgroup label="Release Notes">
146
- <option value="upgrading_ruby_on_rails.html">Upgrading Ruby on Rails</option>
147
- <option value="4_1_release_notes.html">Ruby on Rails 4.1 Release Notes</option>
148
- <option value="4_0_release_notes.html">Ruby on Rails 4.0 Release Notes</option>
149
- <option value="3_2_release_notes.html">Ruby on Rails 3.2 Release Notes</option>
150
- <option value="3_1_release_notes.html">Ruby on Rails 3.1 Release Notes</option>
151
- <option value="3_0_release_notes.html">Ruby on Rails 3.0 Release Notes</option>
152
- <option value="2_3_release_notes.html">Ruby on Rails 2.3 Release Notes</option>
153
- <option value="2_2_release_notes.html">Ruby on Rails 2.2 Release Notes</option>
154
- </optgroup>
155
- </select>
156
- </li>
157
- </ul>
158
- </div>
159
- </div>
160
- <hr class="hide" />
161
-
162
- <div id="feature">
163
- <div class="wrapper">
164
- <h2>Active Model Basics</h2><p>This guide should provide you with all you need to get started using model classes. Active Model allows for Action Pack helpers to interact with non-Active Record models. Active Model also helps building custom ORMs for use outside of the Rails framework.</p><p>After reading this guide, you will know:</p>
165
-
166
- <div id="subCol">
167
- <h3 class="chapter"><img src="images/chapters_icon.gif" alt="" />Chapters</h3>
168
- <ol class="chapters">
169
- <li>
170
- <a href="#introduction">Introduction</a>
171
-
172
- <ul>
173
- <li><a href="#attributemethods">AttributeMethods</a></li>
174
- <li><a href="#callbacks">Callbacks</a></li>
175
- <li><a href="#conversion">Conversion</a></li>
176
- <li><a href="#dirty">Dirty</a></li>
177
- <li><a href="#validations">Validations</a></li>
178
- <li><a href="#activemodel::naming">ActiveModel::Naming</a></li>
179
- </ul>
180
- </li>
181
- </ol>
182
-
183
- </div>
184
-
185
- </div>
186
- </div>
187
-
188
- <div id="container">
189
- <div class="wrapper">
190
- <div id="mainCol">
191
- <h3 id="introduction">1 Introduction</h3><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><h4 id="attributemethods">1.1 AttributeMethods</h4><p>The AttributeMethods module can add custom prefixes and suffixes on methods of a class. It is used by defining the prefixes and suffixes and which methods on the object will use them.</p><div class="code_container">
192
- <pre class="brush: ruby; gutter: false; toolbar: false">
193
- class Person
194
- include ActiveModel::AttributeMethods
195
-
196
- attribute_method_prefix 'reset_'
197
- attribute_method_suffix '_highest?'
198
- define_attribute_methods 'age'
199
-
200
- attr_accessor :age
201
-
202
- private
203
- def reset_attribute(attribute)
204
- send("#{attribute}=", 0)
205
- end
206
-
207
- def attribute_highest?(attribute)
208
- send(attribute) &gt; 100
209
- end
210
- end
211
-
212
- person = Person.new
213
- person.age = 110
214
- person.age_highest? # true
215
- person.reset_age # 0
216
- person.age_highest? # false
217
-
218
- </pre>
219
- </div>
220
- <h4 id="callbacks">1.2 Callbacks</h4><p>Callbacks gives Active Record style callbacks. This provides an ability to define callbacks which run at appropriate times. After defining callbacks, you can wrap them with before, after and around custom methods.</p><div class="code_container">
221
- <pre class="brush: ruby; gutter: false; toolbar: false">
222
- class Person
223
- extend ActiveModel::Callbacks
224
-
225
- define_model_callbacks :update
226
-
227
- before_update :reset_me
228
-
229
- def update
230
- run_callbacks(:update) do
231
- # This method is called when update is called on an object.
232
- end
233
- end
234
-
235
- def reset_me
236
- # This method is called when update is called on an object as a before_update callback is defined.
237
- end
238
- end
239
-
240
- </pre>
241
- </div>
242
- <h4 id="conversion">1.3 Conversion</h4><p>If a class defines <code>persisted?</code> and <code>id</code> methods, then you can include the <code>Conversion</code> module in that class and call the Rails conversion methods on objects of that class.</p><div class="code_container">
243
- <pre class="brush: ruby; gutter: false; toolbar: false">
244
- class Person
245
- include ActiveModel::Conversion
246
-
247
- def persisted?
248
- false
249
- end
250
-
251
- def id
252
- nil
253
- end
254
- end
255
-
256
- person = Person.new
257
- person.to_model == person # =&gt; true
258
- person.to_key # =&gt; nil
259
- person.to_param # =&gt; nil
260
-
261
- </pre>
262
- </div>
263
- <h4 id="dirty">1.4 Dirty</h4><p>An object becomes dirty when it has gone through one or more changes to its attributes and has not been saved. This gives the ability to check whether an object has been changed or not. It also has attribute based accessor methods. Let's consider a Person class with attributes <code>first_name</code> and <code>last_name</code>:</p><div class="code_container">
264
- <pre class="brush: ruby; gutter: false; toolbar: false">
265
- require 'active_model'
266
-
267
- class Person
268
- include ActiveModel::Dirty
269
- define_attribute_methods :first_name, :last_name
270
-
271
- def first_name
272
- @first_name
273
- end
274
-
275
- def first_name=(value)
276
- first_name_will_change!
277
- @first_name = value
278
- end
279
-
280
- def last_name
281
- @last_name
282
- end
283
-
284
- def last_name=(value)
285
- last_name_will_change!
286
- @last_name = value
287
- end
288
-
289
- def save
290
- # do save work...
291
- changes_applied
292
- end
293
- end
294
-
295
- </pre>
296
- </div>
297
- <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><div class="code_container">
298
- <pre class="brush: ruby; gutter: false; toolbar: false">
299
- person = Person.new
300
- person.changed? # =&gt; false
301
-
302
- person.first_name = "First Name"
303
- person.first_name # =&gt; "First Name"
304
-
305
- # returns if any attribute has changed.
306
- person.changed? # =&gt; true
307
-
308
- # returns a list of attributes that have changed before saving.
309
- person.changed # =&gt; ["first_name"]
310
-
311
- # returns a hash of the attributes that have changed with their original values.
312
- person.changed_attributes # =&gt; {"first_name"=&gt;nil}
313
-
314
- # 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.
315
- person.changes # =&gt; {"first_name"=&gt;[nil, "First Name"]}
316
-
317
- </pre>
318
- </div>
319
- <h5 id="attribute-based-accessor-methods">1.4.2 Attribute based accessor methods</h5><p>Track whether the particular attribute has been changed or not.</p><div class="code_container">
320
- <pre class="brush: ruby; gutter: false; toolbar: false">
321
- # attr_name_changed?
322
- person.first_name # =&gt; "First Name"
323
- person.first_name_changed? # =&gt; true
324
-
325
- </pre>
326
- </div>
327
- <p>Track what was the previous value of the attribute.</p><div class="code_container">
328
- <pre class="brush: ruby; gutter: false; toolbar: false">
329
- # attr_name_was accessor
330
- person.first_name_was # =&gt; "First Name"
331
-
332
- </pre>
333
- </div>
334
- <p>Track both previous and current value of the changed attribute. Returns an array if changed, else returns nil.</p><div class="code_container">
335
- <pre class="brush: ruby; gutter: false; toolbar: false">
336
- # attr_name_change
337
- person.first_name_change # =&gt; [nil, "First Name"]
338
- person.last_name_change # =&gt; nil
339
-
340
- </pre>
341
- </div>
342
- <h4 id="validations">1.5 Validations</h4><p>Validations module adds the ability to class objects to validate them in Active Record style.</p><div class="code_container">
343
- <pre class="brush: ruby; gutter: false; toolbar: false">
344
- class Person
345
- include ActiveModel::Validations
346
-
347
- attr_accessor :name, :email, :token
348
-
349
- validates :name, presence: true
350
- validates_format_of :email, with: /\A([^\s]+)((?:[-a-z0-9]\.)[a-z]{2,})\z/i
351
- validates! :token, presence: true
352
- end
353
-
354
- person = Person.new(token: "2b1f325")
355
- person.valid? # =&gt; false
356
- person.name = 'vishnu'
357
- person.email = 'me'
358
- person.valid? # =&gt; false
359
- person.email = 'me@vishnuatrai.com'
360
- person.valid? # =&gt; true
361
- person.token = nil
362
- person.valid? # =&gt; raises ActiveModel::StrictValidationFailed
363
-
364
- </pre>
365
- </div>
366
- <h4 id="activemodel::naming">1.6 ActiveModel::Naming</h4><p>Naming adds a number of class methods which make the naming and routing
367
- easier to manage. The module defines the <code>model_name</code> class method which
368
- will define a number of accessors using some <code>ActiveSupport::Inflector</code> methods.</p><div class="code_container">
369
- <pre class="brush: ruby; gutter: false; toolbar: false">
370
- class Person
371
- extend ActiveModel::Naming
372
- end
373
-
374
- Person.model_name.name # =&gt; "Person"
375
- Person.model_name.singular # =&gt; "person"
376
- Person.model_name.plural # =&gt; "people"
377
- Person.model_name.element # =&gt; "person"
378
- Person.model_name.human # =&gt; "Person"
379
- Person.model_name.collection # =&gt; "people"
380
- Person.model_name.param_key # =&gt; "person"
381
- Person.model_name.i18n_key # =&gt; :person
382
- Person.model_name.route_key # =&gt; "people"
383
- Person.model_name.singular_route_key # =&gt; "person"
384
-
385
- </pre>
386
- </div>
387
-
388
-
389
- <h3>Feedback</h3>
390
- <p>
391
- You're encouraged to help improve the quality of this guide.
392
- </p>
393
- <p>
394
- Please contribute if you see any typos or factual errors.
395
- To get started, you can read our <a href="http://edgeguides.rubyonrails.org/contributing_to_ruby_on_rails.html#contributing-to-the-rails-documentation">documentation contributions</a> section.
396
- </p>
397
- <p>
398
- You may also find incomplete content, or stuff that is not up to date.
399
- Please do add any missing documentation for master. Make sure to check
400
- <a href="http://edgeguides.rubyonrails.org">Edge Guides</a> first to verify
401
- if the issues are already fixed or not on the master branch.
402
- Check the <a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a>
403
- for style and conventions.
404
- </p>
405
- <p>
406
- If for whatever reason you spot something to fix but cannot patch it yourself, please
407
- <a href="https://github.com/rails/rails/issues">open an issue</a>.
408
- </p>
409
- <p>And last but not least, any kind of discussion regarding Ruby on Rails
410
- documentation is very welcome in the <a href="http://groups.google.com/group/rubyonrails-docs">rubyonrails-docs mailing list</a>.
411
- </p>
412
- </div>
413
- </div>
414
- </div>
415
-
416
- <hr class="hide" />
417
- <div id="footer">
418
- <div class="wrapper">
419
- <p>This work is licensed under a <a href="https://creativecommons.org/licenses/by-sa/4.0/">Creative Commons Attribution-ShareAlike 4.0 International</a> License</p>
420
- <p>"Rails", "Ruby on Rails", and the Rails logo are trademarks of David Heinemeier Hansson. All rights reserved.</p>
421
-
422
- </div>
423
- </div>
424
-
425
- <script type="text/javascript" src="javascripts/jquery.min.js"></script>
426
- <script type="text/javascript" src="javascripts/responsive-tables.js"></script>
427
- <script type="text/javascript" src="javascripts/guides.js"></script>
428
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shCore.js"></script>
429
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushRuby.js"></script>
430
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushXml.js"></script>
431
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushSql.js"></script>
432
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushPlain.js"></script>
433
- <script type="text/javascript">
434
- SyntaxHighlighter.all();
435
- $(guidesIndex.bind);
436
- </script>
437
- </body>
438
- </html>
@@ -1,594 +0,0 @@
1
- <!DOCTYPE html>
2
-
3
- <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
4
- <head>
5
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
6
- <meta name="viewport" content="width=device-width, initial-scale=1"/>
7
-
8
- <title>Active Record Basics — Ruby on Rails Guides</title>
9
- <link rel="stylesheet" type="text/css" href="stylesheets/style.css" />
10
- <link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print" />
11
-
12
- <link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shCore.css" />
13
- <link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shThemeRailsGuides.css" />
14
-
15
- <link rel="stylesheet" type="text/css" href="stylesheets/fixes.css" />
16
-
17
- <link href="images/favicon.ico" rel="shortcut icon" type="image/x-icon" />
18
- </head>
19
- <body class="guide">
20
- <div id="topNav">
21
- <div class="wrapper">
22
- <strong class="more-info-label">More at <a href="http://rubyonrails.org/">rubyonrails.org:</a> </strong>
23
- <span class="red-button more-info-button">
24
- More Ruby on Rails
25
- </span>
26
- <ul class="more-info-links s-hidden">
27
- <li class="more-info"><a href="http://rubyonrails.org/">Overview</a></li>
28
- <li class="more-info"><a href="http://rubyonrails.org/download">Download</a></li>
29
- <li class="more-info"><a href="http://rubyonrails.org/deploy">Deploy</a></li>
30
- <li class="more-info"><a href="https://github.com/rails/rails">Code</a></li>
31
- <li class="more-info"><a href="http://rubyonrails.org/screencasts">Screencasts</a></li>
32
- <li class="more-info"><a href="http://rubyonrails.org/documentation">Documentation</a></li>
33
- <li class="more-info"><a href="http://rubyonrails.org/community">Community</a></li>
34
- <li class="more-info"><a href="http://weblog.rubyonrails.org/">Blog</a></li>
35
- </ul>
36
- </div>
37
- </div>
38
- <div id="header">
39
- <div class="wrapper clearfix">
40
- <h1><a href="index.html" title="Return to home page">Guides.rubyonrails.org</a></h1>
41
- <ul class="nav">
42
- <li><a class="nav-item" href="index.html">Home</a></li>
43
- <li class="guides-index guides-index-large">
44
- <a href="index.html" id="guidesMenu" class="guides-index-item nav-item">Guides Index</a>
45
- <div id="guides" class="clearfix" style="display: none;">
46
- <hr />
47
- <dl class="L">
48
- <dt>Start Here</dt>
49
- <dd><a href="getting_started.html">Getting Started with Rails</a></dd>
50
- <dt>Models</dt>
51
- <dd><a href="active_record_basics.html">Active Record Basics</a></dd>
52
- <dd><a href="active_record_migrations.html">Active Record Migrations</a></dd>
53
- <dd><a href="active_record_validations.html">Active Record Validations</a></dd>
54
- <dd><a href="active_record_callbacks.html">Active Record Callbacks</a></dd>
55
- <dd><a href="association_basics.html">Active Record Associations</a></dd>
56
- <dd><a href="active_record_querying.html">Active Record Query Interface</a></dd>
57
- <dt>Views</dt>
58
- <dd><a href="layouts_and_rendering.html">Layouts and Rendering in Rails</a></dd>
59
- <dd><a href="form_helpers.html">Action View Form Helpers</a></dd>
60
- <dt>Controllers</dt>
61
- <dd><a href="action_controller_overview.html">Action Controller Overview</a></dd>
62
- <dd><a href="routing.html">Rails Routing from the Outside In</a></dd>
63
- </dl>
64
- <dl class="R">
65
- <dt>Digging Deeper</dt>
66
- <dd><a href="active_support_core_extensions.html">Active Support Core Extensions</a></dd>
67
- <dd><a href="i18n.html">Rails Internationalization API</a></dd>
68
- <dd><a href="action_mailer_basics.html">Action Mailer Basics</a></dd>
69
- <dd><a href="active_job_basics.html">Active Job Basics</a></dd>
70
- <dd><a href="security.html">Securing Rails Applications</a></dd>
71
- <dd><a href="debugging_rails_applications.html">Debugging Rails Applications</a></dd>
72
- <dd><a href="configuring.html">Configuring Rails Applications</a></dd>
73
- <dd><a href="command_line.html">Rails Command Line Tools and Rake Tasks</a></dd>
74
- <dd><a href="asset_pipeline.html">Asset Pipeline</a></dd>
75
- <dd><a href="working_with_javascript_in_rails.html">Working with JavaScript in Rails</a></dd>
76
- <dt>Extending Rails</dt>
77
- <dd><a href="rails_on_rack.html">Rails on Rack</a></dd>
78
- <dd><a href="generators.html">Creating and Customizing Rails Generators</a></dd>
79
- <dt>Contributing to Ruby on Rails</dt>
80
- <dd><a href="contributing_to_ruby_on_rails.html">Contributing to Ruby on Rails</a></dd>
81
- <dd><a href="api_documentation_guidelines.html">API Documentation Guidelines</a></dd>
82
- <dd><a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a></dd>
83
- <dt>Maintenance Policy</dt>
84
- <dd><a href="maintenance_policy.html">Maintenance Policy</a></dd>
85
- <dt>Release Notes</dt>
86
- <dd><a href="upgrading_ruby_on_rails.html">Upgrading Ruby on Rails</a></dd>
87
- <dd><a href="4_1_release_notes.html">Ruby on Rails 4.1 Release Notes</a></dd>
88
- <dd><a href="4_0_release_notes.html">Ruby on Rails 4.0 Release Notes</a></dd>
89
- <dd><a href="3_2_release_notes.html">Ruby on Rails 3.2 Release Notes</a></dd>
90
- <dd><a href="3_1_release_notes.html">Ruby on Rails 3.1 Release Notes</a></dd>
91
- <dd><a href="3_0_release_notes.html">Ruby on Rails 3.0 Release Notes</a></dd>
92
- <dd><a href="2_3_release_notes.html">Ruby on Rails 2.3 Release Notes</a></dd>
93
- <dd><a href="2_2_release_notes.html">Ruby on Rails 2.2 Release Notes</a></dd>
94
- </dl>
95
- </div>
96
- </li>
97
- <li><a class="nav-item" href="contributing_to_ruby_on_rails.html">Contribute</a></li>
98
- <li><a class="nav-item" href="credits.html">Credits</a></li>
99
- <li class="guides-index guides-index-small">
100
- <select class="guides-index-item nav-item">
101
- <option value="index.html">Guides Index</option>
102
- <optgroup label="Start Here">
103
- <option value="getting_started.html">Getting Started with Rails</option>
104
- </optgroup>
105
- <optgroup label="Models">
106
- <option value="active_record_basics.html">Active Record Basics</option>
107
- <option value="active_record_migrations.html">Active Record Migrations</option>
108
- <option value="active_record_validations.html">Active Record Validations</option>
109
- <option value="active_record_callbacks.html">Active Record Callbacks</option>
110
- <option value="association_basics.html">Active Record Associations</option>
111
- <option value="active_record_querying.html">Active Record Query Interface</option>
112
- </optgroup>
113
- <optgroup label="Views">
114
- <option value="layouts_and_rendering.html">Layouts and Rendering in Rails</option>
115
- <option value="form_helpers.html">Action View Form Helpers</option>
116
- </optgroup>
117
- <optgroup label="Controllers">
118
- <option value="action_controller_overview.html">Action Controller Overview</option>
119
- <option value="routing.html">Rails Routing from the Outside In</option>
120
- </optgroup>
121
- <optgroup label="Digging Deeper">
122
- <option value="active_support_core_extensions.html">Active Support Core Extensions</option>
123
- <option value="i18n.html">Rails Internationalization API</option>
124
- <option value="action_mailer_basics.html">Action Mailer Basics</option>
125
- <option value="active_job_basics.html">Active Job Basics</option>
126
- <option value="security.html">Securing Rails Applications</option>
127
- <option value="debugging_rails_applications.html">Debugging Rails Applications</option>
128
- <option value="configuring.html">Configuring Rails Applications</option>
129
- <option value="command_line.html">Rails Command Line Tools and Rake Tasks</option>
130
- <option value="asset_pipeline.html">Asset Pipeline</option>
131
- <option value="working_with_javascript_in_rails.html">Working with JavaScript in Rails</option>
132
- </optgroup>
133
- <optgroup label="Extending Rails">
134
- <option value="rails_on_rack.html">Rails on Rack</option>
135
- <option value="generators.html">Creating and Customizing Rails Generators</option>
136
- </optgroup>
137
- <optgroup label="Contributing to Ruby on Rails">
138
- <option value="contributing_to_ruby_on_rails.html">Contributing to Ruby on Rails</option>
139
- <option value="api_documentation_guidelines.html">API Documentation Guidelines</option>
140
- <option value="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</option>
141
- </optgroup>
142
- <optgroup label="Maintenance Policy">
143
- <option value="maintenance_policy.html">Maintenance Policy</option>
144
- </optgroup>
145
- <optgroup label="Release Notes">
146
- <option value="upgrading_ruby_on_rails.html">Upgrading Ruby on Rails</option>
147
- <option value="4_1_release_notes.html">Ruby on Rails 4.1 Release Notes</option>
148
- <option value="4_0_release_notes.html">Ruby on Rails 4.0 Release Notes</option>
149
- <option value="3_2_release_notes.html">Ruby on Rails 3.2 Release Notes</option>
150
- <option value="3_1_release_notes.html">Ruby on Rails 3.1 Release Notes</option>
151
- <option value="3_0_release_notes.html">Ruby on Rails 3.0 Release Notes</option>
152
- <option value="2_3_release_notes.html">Ruby on Rails 2.3 Release Notes</option>
153
- <option value="2_2_release_notes.html">Ruby on Rails 2.2 Release Notes</option>
154
- </optgroup>
155
- </select>
156
- </li>
157
- </ul>
158
- </div>
159
- </div>
160
- <hr class="hide" />
161
-
162
- <div id="feature">
163
- <div class="wrapper">
164
- <h2>Active Record Basics</h2><p>This guide is an introduction to Active Record.</p><p>After reading this guide, you will know:</p>
165
- <ul>
166
- <li>What Object Relational Mapping and Active Record are and how they are used in
167
- Rails.</li>
168
- <li>How Active Record fits into the Model-View-Controller paradigm.</li>
169
- <li>How to use Active Record models to manipulate data stored in a relational
170
- database.</li>
171
- <li>Active Record schema naming conventions.</li>
172
- <li>The concepts of database migrations, validations and callbacks.</li>
173
- </ul>
174
-
175
-
176
- <div id="subCol">
177
- <h3 class="chapter"><img src="images/chapters_icon.gif" alt="" />Chapters</h3>
178
- <ol class="chapters">
179
- <li>
180
- <a href="#what-is-active-record-questionmark">What is Active Record?</a>
181
-
182
- <ul>
183
- <li><a href="#the-active-record-pattern">The Active Record Pattern</a></li>
184
- <li><a href="#object-relational-mapping">Object Relational Mapping</a></li>
185
- <li><a href="#active-record-as-an-orm-framework">Active Record as an ORM Framework</a></li>
186
- </ul>
187
- </li>
188
- <li>
189
- <a href="#convention-over-configuration-in-active-record">Convention over Configuration in Active Record</a>
190
-
191
- <ul>
192
- <li><a href="#naming-conventions">Naming Conventions</a></li>
193
- <li><a href="#schema-conventions">Schema Conventions</a></li>
194
- </ul>
195
- </li>
196
- <li><a href="#creating-active-record-models">Creating Active Record Models</a></li>
197
- <li><a href="#overriding-the-naming-conventions">Overriding the Naming Conventions</a></li>
198
- <li>
199
- <a href="#crud:-reading-and-writing-data">CRUD: Reading and Writing Data</a>
200
-
201
- <ul>
202
- <li><a href="#create">Create</a></li>
203
- <li><a href="#read">Read</a></li>
204
- <li><a href="#update">Update</a></li>
205
- <li><a href="#delete">Delete</a></li>
206
- </ul>
207
- </li>
208
- <li><a href="#validations">Validations</a></li>
209
- <li><a href="#callbacks">Callbacks</a></li>
210
- <li><a href="#migrations">Migrations</a></li>
211
- </ol>
212
-
213
- </div>
214
-
215
- </div>
216
- </div>
217
-
218
- <div id="container">
219
- <div class="wrapper">
220
- <div id="mainCol">
221
- <h3 id="what-is-active-record-questionmark">1 What is Active Record?</h3><p>Active Record is the M in <a href="getting_started.html#the-mvc-architecture">MVC</a> - the
222
- model - which is the layer of the system responsible for representing business
223
- data and logic. Active Record facilitates the creation and use of business
224
- objects whose data requires persistent storage to a database. It is an
225
- implementation of the Active Record pattern which itself is a description of an
226
- Object Relational Mapping system.</p><h4 id="the-active-record-pattern">1.1 The Active Record Pattern</h4><p><a href="http://www.martinfowler.com/eaaCatalog/activeRecord.html">Active Record was described by Martin Fowler</a>
227
- in his book <em>Patterns of Enterprise Application Architecture</em>. In
228
- Active Record, objects carry both persistent data and behavior which
229
- operates on that data. Active Record takes the opinion that ensuring
230
- data access logic is part of the object will educate users of that
231
- object on how to write to and read from the database.</p><h4 id="object-relational-mapping">1.2 Object Relational Mapping</h4><p>Object-Relational Mapping, commonly referred to as its abbreviation ORM, is
232
- a technique that connects the rich objects of an application to tables in
233
- a relational database management system. Using ORM, the properties and
234
- relationships of the objects in an application can be easily stored and
235
- retrieved from a database without writing SQL statements directly and with less
236
- overall database access code.</p><h4 id="active-record-as-an-orm-framework">1.3 Active Record as an ORM Framework</h4><p>Active Record gives us several mechanisms, the most important being the ability
237
- to:</p>
238
- <ul>
239
- <li>Represent models and their data.</li>
240
- <li>Represent associations between these models.</li>
241
- <li>Represent inheritance hierarchies through related models.</li>
242
- <li>Validate models before they get persisted to the database.</li>
243
- <li>Perform database operations in an object-oriented fashion.</li>
244
- </ul>
245
- <h3 id="convention-over-configuration-in-active-record">2 Convention over Configuration in Active Record</h3><p>When writing applications using other programming languages or frameworks, it
246
- may be necessary to write a lot of configuration code. This is particularly true
247
- for ORM frameworks in general. However, if you follow the conventions adopted by
248
- Rails, you'll need to write very little configuration (in some case no
249
- configuration at all) when creating Active Record models. The idea is that if
250
- you configure your applications in the very same way most of the time then this
251
- should be the default way. Thus, explicit configuration would be needed
252
- only in those cases where you can't follow the standard convention.</p><h4 id="naming-conventions">2.1 Naming Conventions</h4><p>By default, Active Record uses some naming conventions to find out how the
253
- mapping between models and database tables should be created. Rails will
254
- pluralize your class names to find the respective database table. So, for
255
- a class <code>Book</code>, you should have a database table called <strong>books</strong>. The Rails
256
- pluralization mechanisms are very powerful, being capable to pluralize (and
257
- singularize) both regular and irregular words. When using class names composed
258
- of two or more words, the model class name should follow the Ruby conventions,
259
- using the CamelCase form, while the table name must contain the words separated
260
- by underscores. Examples:</p>
261
- <ul>
262
- <li>Database Table - Plural with underscores separating words (e.g., <code>book_clubs</code>).</li>
263
- <li>Model Class - Singular with the first letter of each word capitalized (e.g.,
264
- <code>BookClub</code>).</li>
265
- </ul>
266
-
267
- <table>
268
- <thead>
269
- <tr>
270
- <th>Model / Class</th>
271
- <th>Table / Schema</th>
272
- </tr>
273
- </thead>
274
- <tbody>
275
- <tr>
276
- <td><code>Article</code></td>
277
- <td><code>articles</code></td>
278
- </tr>
279
- <tr>
280
- <td><code>LineItem</code></td>
281
- <td><code>line_items</code></td>
282
- </tr>
283
- <tr>
284
- <td><code>Deer</code></td>
285
- <td><code>deers</code></td>
286
- </tr>
287
- <tr>
288
- <td><code>Mouse</code></td>
289
- <td><code>mice</code></td>
290
- </tr>
291
- <tr>
292
- <td><code>Person</code></td>
293
- <td><code>people</code></td>
294
- </tr>
295
- </tbody>
296
- </table>
297
- <h4 id="schema-conventions">2.2 Schema Conventions</h4><p>Active Record uses naming conventions for the columns in database tables,
298
- depending on the purpose of these columns.</p>
299
- <ul>
300
- <li>
301
- <strong>Foreign keys</strong> - These fields should be named following the pattern
302
- <code>singularized_table_name_id</code> (e.g., <code>item_id</code>, <code>order_id</code>). These are the
303
- fields that Active Record will look for when you create associations between
304
- your models.</li>
305
- <li>
306
- <strong>Primary keys</strong> - By default, Active Record will use an integer column named
307
- <code>id</code> as the table's primary key. When using <a href="migrations.html">Active Record
308
- Migrations</a> to create your tables, this column will be
309
- automatically created.</li>
310
- </ul>
311
- <p>There are also some optional column names that will add additional features
312
- to Active Record instances:</p>
313
- <ul>
314
- <li>
315
- <code>created_at</code> - Automatically gets set to the current date and time when the
316
- record is first created.</li>
317
- <li>
318
- <code>updated_at</code> - Automatically gets set to the current date and time whenever
319
- the record is updated.</li>
320
- <li>
321
- <code>lock_version</code> - Adds <a href="http://api.rubyonrails.org/classes/ActiveRecord/Locking.html">optimistic
322
- locking</a> to
323
- a model.</li>
324
- <li>
325
- <code>type</code> - Specifies that the model uses <a href="http://api.rubyonrails.org/classes/ActiveRecord/Base.html#label-Single+table+inheritance">Single Table
326
- Inheritance</a>.</li>
327
- <li>
328
- <code>(association_name)_type</code> - Stores the type for
329
- <a href="association_basics.html#polymorphic-associations">polymorphic associations</a>.</li>
330
- <li>
331
- <code>(table_name)_count</code> - Used to cache the number of belonging objects on
332
- associations. For example, a <code>comments_count</code> column in a <code>Articles</code> class that
333
- has many instances of <code>Comment</code> will cache the number of existent comments
334
- for each article.</li>
335
- </ul>
336
- <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, <code>type</code> is a reserved keyword used to designate a table using Single Table Inheritance (STI). If you are not using STI, try an analogous keyword like "context", that may still accurately describe the data you are modeling.</p></div><h3 id="creating-active-record-models">3 Creating Active Record Models</h3><p>It is very easy to create Active Record models. All you have to do is to
337
- subclass the <code>ActiveRecord::Base</code> class and you're good to go:</p><div class="code_container">
338
- <pre class="brush: ruby; gutter: false; toolbar: false">
339
- class Product &lt; ActiveRecord::Base
340
- end
341
-
342
- </pre>
343
- </div>
344
- <p>This will create a <code>Product</code> model, mapped to a <code>products</code> table at the
345
- database. By doing this you'll also have the ability to map the columns of each
346
- row in that table with the attributes of the instances of your model. Suppose
347
- that the <code>products</code> table was created using an SQL sentence like:</p><div class="code_container">
348
- <pre class="brush: sql; gutter: false; toolbar: false">
349
- CREATE TABLE products (
350
- id int(11) NOT NULL auto_increment,
351
- name varchar(255),
352
- PRIMARY KEY (id)
353
- );
354
-
355
- </pre>
356
- </div>
357
- <p>Following the table schema above, you would be able to write code like the
358
- following:</p><div class="code_container">
359
- <pre class="brush: ruby; gutter: false; toolbar: false">
360
- p = Product.new
361
- p.name = "Some Book"
362
- puts p.name # "Some Book"
363
-
364
- </pre>
365
- </div>
366
- <h3 id="overriding-the-naming-conventions">4 Overriding the Naming Conventions</h3><p>What if you need to follow a different naming convention or need to use your
367
- Rails application with a legacy database? No problem, you can easily override
368
- the default conventions.</p><p>You can use the <code>ActiveRecord::Base.table_name=</code> method to specify the table
369
- name that should be used:</p><div class="code_container">
370
- <pre class="brush: ruby; gutter: false; toolbar: false">
371
- class Product &lt; ActiveRecord::Base
372
- self.table_name = "PRODUCT"
373
- end
374
-
375
- </pre>
376
- </div>
377
- <p>If you do so, you will have to define manually the class name that is hosting
378
- the fixtures (class_name.yml) using the <code>set_fixture_class</code> method in your test
379
- definition:</p><div class="code_container">
380
- <pre class="brush: ruby; gutter: false; toolbar: false">
381
- class FunnyJoke &lt; ActiveSupport::TestCase
382
- set_fixture_class funny_jokes: Joke
383
- fixtures :funny_jokes
384
- ...
385
- end
386
-
387
- </pre>
388
- </div>
389
- <p>It's also possible to override the column that should be used as the table's
390
- primary key using the <code>ActiveRecord::Base.primary_key=</code> method:</p><div class="code_container">
391
- <pre class="brush: ruby; gutter: false; toolbar: false">
392
- class Product &lt; ActiveRecord::Base
393
- self.primary_key = "product_id"
394
- end
395
-
396
- </pre>
397
- </div>
398
- <h3 id="crud:-reading-and-writing-data">5 CRUD: Reading and Writing Data</h3><p>CRUD is an acronym for the four verbs we use to operate on data: <strong>C</strong>reate,
399
- <strong>R</strong>ead, <strong>U</strong>pdate and <strong>D</strong>elete. Active Record automatically creates methods
400
- to allow an application to read and manipulate data stored within its tables.</p><h4 id="create">5.1 Create</h4><p>Active Record objects can be created from a hash, a block or have their
401
- attributes manually set after creation. The <code>new</code> method will return a new
402
- object while <code>create</code> will return the object and save it to the database.</p><p>For example, given a model <code>User</code> with attributes of <code>name</code> and <code>occupation</code>,
403
- the <code>create</code> method call will create and save a new record into the database:</p><div class="code_container">
404
- <pre class="brush: ruby; gutter: false; toolbar: false">
405
- user = User.create(name: "David", occupation: "Code Artist")
406
-
407
- </pre>
408
- </div>
409
- <p>Using the <code>new</code> method, an object can be instantiated without being saved:</p><div class="code_container">
410
- <pre class="brush: ruby; gutter: false; toolbar: false">
411
- user = User.new
412
- user.name = "David"
413
- user.occupation = "Code Artist"
414
-
415
- </pre>
416
- </div>
417
- <p>A call to <code>user.save</code> will commit the record to the database.</p><p>Finally, if a block is provided, both <code>create</code> and <code>new</code> will yield the new
418
- object to that block for initialization:</p><div class="code_container">
419
- <pre class="brush: ruby; gutter: false; toolbar: false">
420
- user = User.new do |u|
421
- u.name = "David"
422
- u.occupation = "Code Artist"
423
- end
424
-
425
- </pre>
426
- </div>
427
- <h4 id="read">5.2 Read</h4><p>Active Record provides a rich API for accessing data within a database. Below
428
- are a few examples of different data access methods provided by Active Record.</p><div class="code_container">
429
- <pre class="brush: ruby; gutter: false; toolbar: false">
430
- # return a collection with all users
431
- users = User.all
432
-
433
- </pre>
434
- </div>
435
- <div class="code_container">
436
- <pre class="brush: ruby; gutter: false; toolbar: false">
437
- # return the first user
438
- user = User.first
439
-
440
- </pre>
441
- </div>
442
- <div class="code_container">
443
- <pre class="brush: ruby; gutter: false; toolbar: false">
444
- # return the first user named David
445
- david = User.find_by(name: 'David')
446
-
447
- </pre>
448
- </div>
449
- <div class="code_container">
450
- <pre class="brush: ruby; gutter: false; toolbar: false">
451
- # find all users named David who are Code Artists and sort by created_at in reverse chronological order
452
- users = User.where(name: 'David', occupation: 'Code Artist').order('created_at DESC')
453
-
454
- </pre>
455
- </div>
456
- <p>You can learn more about querying an Active Record model in the <a href="active_record_querying.html">Active Record
457
- Query Interface</a> guide.</p><h4 id="update">5.3 Update</h4><p>Once an Active Record object has been retrieved, its attributes can be modified
458
- and it can be saved to the database.</p><div class="code_container">
459
- <pre class="brush: ruby; gutter: false; toolbar: false">
460
- user = User.find_by(name: 'David')
461
- user.name = 'Dave'
462
- user.save
463
-
464
- </pre>
465
- </div>
466
- <p>A shorthand for this is to use a hash mapping attribute names to the desired
467
- value, like so:</p><div class="code_container">
468
- <pre class="brush: ruby; gutter: false; toolbar: false">
469
- user = User.find_by(name: 'David')
470
- user.update(name: 'Dave')
471
-
472
- </pre>
473
- </div>
474
- <p>This is most useful when updating several attributes at once. If, on the other
475
- hand, you'd like to update several records in bulk, you may find the
476
- <code>update_all</code> class method useful:</p><div class="code_container">
477
- <pre class="brush: ruby; gutter: false; toolbar: false">
478
- User.update_all "max_login_attempts = 3, must_change_password = 'true'"
479
-
480
- </pre>
481
- </div>
482
- <h4 id="delete">5.4 Delete</h4><p>Likewise, once retrieved an Active Record object can be destroyed which removes
483
- it from the database.</p><div class="code_container">
484
- <pre class="brush: ruby; gutter: false; toolbar: false">
485
- user = User.find_by(name: 'David')
486
- user.destroy
487
-
488
- </pre>
489
- </div>
490
- <h3 id="validations">6 Validations</h3><p>Active Record allows you to validate the state of a model before it gets written
491
- into the database. There are several methods that you can use to check your
492
- models and validate that an attribute value is not empty, is unique and not
493
- already in the database, follows a specific format and many more.</p><p>Validation is a very important issue to consider when persisting to the database, so
494
- the methods <code>save</code> and <code>update</code> take it into account when
495
- running: they return <code>false</code> when validation fails and they didn't actually
496
- perform any operation on the database. All of these have a bang counterpart (that
497
- is, <code>save!</code> and <code>update!</code>), which are stricter in that
498
- they raise the exception <code>ActiveRecord::RecordInvalid</code> if validation fails.
499
- A quick example to illustrate:</p><div class="code_container">
500
- <pre class="brush: ruby; gutter: false; toolbar: false">
501
- class User &lt; ActiveRecord::Base
502
- validates :name, presence: true
503
- end
504
-
505
- user = User.new
506
- user.save # =&gt; false
507
- user.save! # =&gt; ActiveRecord::RecordInvalid: Validation failed: Name can't be blank
508
-
509
- </pre>
510
- </div>
511
- <p>You can learn more about validations in the <a href="active_record_validations.html">Active Record Validations
512
- guide</a>.</p><h3 id="callbacks">7 Callbacks</h3><p>Active Record callbacks allow you to attach code to certain events in the
513
- life-cycle of your models. This enables you to add behavior to your models by
514
- transparently executing code when those events occur, like when you create a new
515
- record, update it, destroy it and so on. You can learn more about callbacks in
516
- the <a href="active_record_callbacks.html">Active Record Callbacks guide</a>.</p><h3 id="migrations">8 Migrations</h3><p>Rails provides a domain-specific language for managing a database schema called
517
- migrations. Migrations are stored in files which are executed against any
518
- database that Active Record supports using <code>rake</code>. Here's a migration that
519
- creates a table:</p><div class="code_container">
520
- <pre class="brush: ruby; gutter: false; toolbar: false">
521
- class CreatePublications &lt; ActiveRecord::Migration
522
- def change
523
- create_table :publications do |t|
524
- t.string :title
525
- t.text :description
526
- t.references :publication_type
527
- t.integer :publisher_id
528
- t.string :publisher_type
529
- t.boolean :single_issue
530
-
531
- t.timestamps
532
- end
533
- add_index :publications, :publication_type_id
534
- end
535
- end
536
-
537
- </pre>
538
- </div>
539
- <p>Rails keeps track of which files have been committed to the database and
540
- provides rollback features. To actually create the table, you'd run <code>rake db:migrate</code>
541
- and to roll it back, <code>rake db:rollback</code>.</p><p>Note that the above code is database-agnostic: it will run in MySQL,
542
- PostgreSQL, Oracle and others. You can learn more about migrations in the
543
- <a href="migrations.html">Active Record Migrations guide</a>.</p>
544
-
545
- <h3>Feedback</h3>
546
- <p>
547
- You're encouraged to help improve the quality of this guide.
548
- </p>
549
- <p>
550
- Please contribute if you see any typos or factual errors.
551
- To get started, you can read our <a href="http://edgeguides.rubyonrails.org/contributing_to_ruby_on_rails.html#contributing-to-the-rails-documentation">documentation contributions</a> section.
552
- </p>
553
- <p>
554
- You may also find incomplete content, or stuff that is not up to date.
555
- Please do add any missing documentation for master. Make sure to check
556
- <a href="http://edgeguides.rubyonrails.org">Edge Guides</a> first to verify
557
- if the issues are already fixed or not on the master branch.
558
- Check the <a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a>
559
- for style and conventions.
560
- </p>
561
- <p>
562
- If for whatever reason you spot something to fix but cannot patch it yourself, please
563
- <a href="https://github.com/rails/rails/issues">open an issue</a>.
564
- </p>
565
- <p>And last but not least, any kind of discussion regarding Ruby on Rails
566
- documentation is very welcome in the <a href="http://groups.google.com/group/rubyonrails-docs">rubyonrails-docs mailing list</a>.
567
- </p>
568
- </div>
569
- </div>
570
- </div>
571
-
572
- <hr class="hide" />
573
- <div id="footer">
574
- <div class="wrapper">
575
- <p>This work is licensed under a <a href="https://creativecommons.org/licenses/by-sa/4.0/">Creative Commons Attribution-ShareAlike 4.0 International</a> License</p>
576
- <p>"Rails", "Ruby on Rails", and the Rails logo are trademarks of David Heinemeier Hansson. All rights reserved.</p>
577
-
578
- </div>
579
- </div>
580
-
581
- <script type="text/javascript" src="javascripts/jquery.min.js"></script>
582
- <script type="text/javascript" src="javascripts/responsive-tables.js"></script>
583
- <script type="text/javascript" src="javascripts/guides.js"></script>
584
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shCore.js"></script>
585
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushRuby.js"></script>
586
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushXml.js"></script>
587
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushSql.js"></script>
588
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushPlain.js"></script>
589
- <script type="text/javascript">
590
- SyntaxHighlighter.all();
591
- $(guidesIndex.bind);
592
- </script>
593
- </body>
594
- </html>