railties 3.0.0.beta

Sign up to get free protection for your applications and to get access to all the features.
Files changed (334) hide show
  1. data/CHANGELOG +2201 -0
  2. data/README +286 -0
  3. data/bin/rails +11 -0
  4. data/builtin/rails_info/rails/info.rb +147 -0
  5. data/builtin/rails_info/rails/info_controller.rb +15 -0
  6. data/builtin/rails_info/rails/info_helper.rb +2 -0
  7. data/builtin/routes.rb +3 -0
  8. data/guides/files/javascripts/code_highlighter.js +188 -0
  9. data/guides/files/javascripts/guides.js +7 -0
  10. data/guides/files/javascripts/highlighters.js +90 -0
  11. data/guides/files/stylesheets/main.css +441 -0
  12. data/guides/files/stylesheets/print.css +52 -0
  13. data/guides/files/stylesheets/reset.css +43 -0
  14. data/guides/files/stylesheets/style.css +13 -0
  15. data/guides/files/stylesheets/syntax.css +31 -0
  16. data/guides/images/belongs_to.png +0 -0
  17. data/guides/images/book_icon.gif +0 -0
  18. data/guides/images/bullet.gif +0 -0
  19. data/guides/images/chapters_icon.gif +0 -0
  20. data/guides/images/check_bullet.gif +0 -0
  21. data/guides/images/credits_pic_blank.gif +0 -0
  22. data/guides/images/csrf.png +0 -0
  23. data/guides/images/customized_error_messages.png +0 -0
  24. data/guides/images/error_messages.png +0 -0
  25. data/guides/images/feature_tile.gif +0 -0
  26. data/guides/images/footer_tile.gif +0 -0
  27. data/guides/images/fxn.png +0 -0
  28. data/guides/images/grey_bullet.gif +0 -0
  29. data/guides/images/habtm.png +0 -0
  30. data/guides/images/has_many.png +0 -0
  31. data/guides/images/has_many_through.png +0 -0
  32. data/guides/images/has_one.png +0 -0
  33. data/guides/images/has_one_through.png +0 -0
  34. data/guides/images/header_backdrop.png +0 -0
  35. data/guides/images/header_tile.gif +0 -0
  36. data/guides/images/i18n/demo_localized_pirate.png +0 -0
  37. data/guides/images/i18n/demo_translated_en.png +0 -0
  38. data/guides/images/i18n/demo_translated_pirate.png +0 -0
  39. data/guides/images/i18n/demo_translation_missing.png +0 -0
  40. data/guides/images/i18n/demo_untranslated.png +0 -0
  41. data/guides/images/icons/README +5 -0
  42. data/guides/images/icons/callouts/1.png +0 -0
  43. data/guides/images/icons/callouts/10.png +0 -0
  44. data/guides/images/icons/callouts/11.png +0 -0
  45. data/guides/images/icons/callouts/12.png +0 -0
  46. data/guides/images/icons/callouts/13.png +0 -0
  47. data/guides/images/icons/callouts/14.png +0 -0
  48. data/guides/images/icons/callouts/15.png +0 -0
  49. data/guides/images/icons/callouts/2.png +0 -0
  50. data/guides/images/icons/callouts/3.png +0 -0
  51. data/guides/images/icons/callouts/4.png +0 -0
  52. data/guides/images/icons/callouts/5.png +0 -0
  53. data/guides/images/icons/callouts/6.png +0 -0
  54. data/guides/images/icons/callouts/7.png +0 -0
  55. data/guides/images/icons/callouts/8.png +0 -0
  56. data/guides/images/icons/callouts/9.png +0 -0
  57. data/guides/images/icons/caution.png +0 -0
  58. data/guides/images/icons/example.png +0 -0
  59. data/guides/images/icons/home.png +0 -0
  60. data/guides/images/icons/important.png +0 -0
  61. data/guides/images/icons/next.png +0 -0
  62. data/guides/images/icons/note.png +0 -0
  63. data/guides/images/icons/prev.png +0 -0
  64. data/guides/images/icons/tip.png +0 -0
  65. data/guides/images/icons/up.png +0 -0
  66. data/guides/images/icons/warning.png +0 -0
  67. data/guides/images/nav_arrow.gif +0 -0
  68. data/guides/images/polymorphic.png +0 -0
  69. data/guides/images/posts_index.png +0 -0
  70. data/guides/images/rails_guides_logo.gif +0 -0
  71. data/guides/images/rails_logo_remix.gif +0 -0
  72. data/guides/images/rails_welcome.png +0 -0
  73. data/guides/images/session_fixation.png +0 -0
  74. data/guides/images/tab_grey.gif +0 -0
  75. data/guides/images/tab_info.gif +0 -0
  76. data/guides/images/tab_note.gif +0 -0
  77. data/guides/images/tab_red.gif +0 -0
  78. data/guides/images/tab_yellow.gif +0 -0
  79. data/guides/images/tab_yellow.png +0 -0
  80. data/guides/images/validation_error_messages.png +0 -0
  81. data/guides/rails_guides.rb +44 -0
  82. data/guides/rails_guides/generator.rb +178 -0
  83. data/guides/rails_guides/helpers.rb +34 -0
  84. data/guides/rails_guides/indexer.rb +55 -0
  85. data/guides/rails_guides/levenshtein.rb +31 -0
  86. data/guides/rails_guides/textile_extensions.rb +41 -0
  87. data/guides/source/2_2_release_notes.textile +422 -0
  88. data/guides/source/2_3_release_notes.textile +610 -0
  89. data/guides/source/3_0_release_notes.textile +560 -0
  90. data/guides/source/action_controller_overview.textile +783 -0
  91. data/guides/source/action_mailer_basics.textile +437 -0
  92. data/guides/source/action_view_overview.textile +1481 -0
  93. data/guides/source/active_record_basics.textile +204 -0
  94. data/guides/source/active_record_querying.textile +961 -0
  95. data/guides/source/active_support_core_extensions.textile +1835 -0
  96. data/guides/source/activerecord_validations_callbacks.textile +1127 -0
  97. data/guides/source/ajax_on_rails.textile +342 -0
  98. data/guides/source/association_basics.textile +1816 -0
  99. data/guides/source/caching_with_rails.textile +390 -0
  100. data/guides/source/command_line.textile +589 -0
  101. data/guides/source/configuring.textile +290 -0
  102. data/guides/source/contribute.textile +71 -0
  103. data/guides/source/contributing_to_rails.textile +272 -0
  104. data/guides/source/credits.textile.erb +52 -0
  105. data/guides/source/debugging_rails_applications.textile +709 -0
  106. data/guides/source/form_helpers.textile +768 -0
  107. data/guides/source/generators.textile +378 -0
  108. data/guides/source/getting_started.textile +1310 -0
  109. data/guides/source/i18n.textile +879 -0
  110. data/guides/source/index.textile.erb +124 -0
  111. data/guides/source/layout.html.erb +103 -0
  112. data/guides/source/layouts_and_rendering.textile +979 -0
  113. data/guides/source/migrations.textile +591 -0
  114. data/guides/source/nested_model_forms.textile +222 -0
  115. data/guides/source/performance_testing.textile +531 -0
  116. data/guides/source/plugins.textile +1512 -0
  117. data/guides/source/rails_application_templates.textile +238 -0
  118. data/guides/source/rails_on_rack.textile +306 -0
  119. data/guides/source/routing.textile +903 -0
  120. data/guides/source/security.textile +984 -0
  121. data/guides/source/testing.textile +946 -0
  122. data/lib/generators/erb.rb +8 -0
  123. data/lib/generators/erb/controller/controller_generator.rb +21 -0
  124. data/lib/generators/erb/controller/templates/view.html.erb +2 -0
  125. data/lib/generators/erb/mailer/mailer_generator.rb +20 -0
  126. data/lib/generators/erb/mailer/templates/view.text.erb +3 -0
  127. data/lib/generators/erb/scaffold/scaffold_generator.rb +53 -0
  128. data/lib/generators/erb/scaffold/templates/_form.html.erb +13 -0
  129. data/lib/generators/erb/scaffold/templates/edit.html.erb +6 -0
  130. data/lib/generators/erb/scaffold/templates/index.html.erb +27 -0
  131. data/lib/generators/erb/scaffold/templates/layout.html.erb +16 -0
  132. data/lib/generators/erb/scaffold/templates/new.html.erb +5 -0
  133. data/lib/generators/erb/scaffold/templates/show.html.erb +10 -0
  134. data/lib/generators/rails/app/USAGE +9 -0
  135. data/lib/generators/rails/app/app_generator.rb +262 -0
  136. data/lib/generators/rails/app/templates/Gemfile +34 -0
  137. data/lib/generators/rails/app/templates/README +243 -0
  138. data/lib/generators/rails/app/templates/Rakefile +10 -0
  139. data/lib/generators/rails/app/templates/app/controllers/application_controller.rb +3 -0
  140. data/lib/generators/rails/app/templates/app/helpers/application_helper.rb +2 -0
  141. data/lib/generators/rails/app/templates/app/models/.empty_directory +0 -0
  142. data/lib/generators/rails/app/templates/app/views/layouts/.empty_directory +0 -0
  143. data/lib/generators/rails/app/templates/config.ru +4 -0
  144. data/lib/generators/rails/app/templates/config/application.rb +51 -0
  145. data/lib/generators/rails/app/templates/config/boot.rb +17 -0
  146. data/lib/generators/rails/app/templates/config/databases/frontbase.yml +28 -0
  147. data/lib/generators/rails/app/templates/config/databases/ibm_db.yml +71 -0
  148. data/lib/generators/rails/app/templates/config/databases/mysql.yml +60 -0
  149. data/lib/generators/rails/app/templates/config/databases/oracle.yml +39 -0
  150. data/lib/generators/rails/app/templates/config/databases/postgresql.yml +51 -0
  151. data/lib/generators/rails/app/templates/config/databases/sqlite3.yml +22 -0
  152. data/lib/generators/rails/app/templates/config/environment.rb +5 -0
  153. data/lib/generators/rails/app/templates/config/environments/development.rb.tt +19 -0
  154. data/lib/generators/rails/app/templates/config/environments/production.rb.tt +33 -0
  155. data/lib/generators/rails/app/templates/config/environments/test.rb.tt +29 -0
  156. data/lib/generators/rails/app/templates/config/initializers/backtrace_silencers.rb +7 -0
  157. data/lib/generators/rails/app/templates/config/initializers/cookie_verification_secret.rb.tt +7 -0
  158. data/lib/generators/rails/app/templates/config/initializers/inflections.rb +10 -0
  159. data/lib/generators/rails/app/templates/config/initializers/mime_types.rb +5 -0
  160. data/lib/generators/rails/app/templates/config/initializers/session_store.rb.tt +15 -0
  161. data/lib/generators/rails/app/templates/config/locales/en.yml +5 -0
  162. data/lib/generators/rails/app/templates/config/routes.rb +58 -0
  163. data/lib/generators/rails/app/templates/db/seeds.rb +7 -0
  164. data/lib/generators/rails/app/templates/doc/README_FOR_APP +2 -0
  165. data/lib/generators/rails/app/templates/gitignore +4 -0
  166. data/lib/generators/rails/app/templates/public/404.html +26 -0
  167. data/lib/generators/rails/app/templates/public/422.html +26 -0
  168. data/lib/generators/rails/app/templates/public/500.html +26 -0
  169. data/lib/generators/rails/app/templates/public/favicon.ico +0 -0
  170. data/lib/generators/rails/app/templates/public/images/rails.png +0 -0
  171. data/lib/generators/rails/app/templates/public/index.html +278 -0
  172. data/lib/generators/rails/app/templates/public/javascripts/application.js +2 -0
  173. data/lib/generators/rails/app/templates/public/javascripts/controls.js +963 -0
  174. data/lib/generators/rails/app/templates/public/javascripts/dragdrop.js +973 -0
  175. data/lib/generators/rails/app/templates/public/javascripts/effects.js +1128 -0
  176. data/lib/generators/rails/app/templates/public/javascripts/prototype.js +4320 -0
  177. data/lib/generators/rails/app/templates/public/javascripts/rails.js +110 -0
  178. data/lib/generators/rails/app/templates/public/robots.txt +5 -0
  179. data/lib/generators/rails/app/templates/public/stylesheets/.empty_directory +0 -0
  180. data/lib/generators/rails/app/templates/script/rails +9 -0
  181. data/lib/generators/rails/app/templates/test/fixtures/.empty_directory +0 -0
  182. data/lib/generators/rails/app/templates/test/functional/.empty_directory +0 -0
  183. data/lib/generators/rails/app/templates/test/integration/.empty_directory +0 -0
  184. data/lib/generators/rails/app/templates/test/performance/browsing_test.rb +9 -0
  185. data/lib/generators/rails/app/templates/test/test_helper.rb +13 -0
  186. data/lib/generators/rails/app/templates/test/unit/.empty_directory +0 -0
  187. data/lib/generators/rails/controller/USAGE +18 -0
  188. data/lib/generators/rails/controller/controller_generator.rb +14 -0
  189. data/lib/generators/rails/controller/templates/controller.rb +7 -0
  190. data/lib/generators/rails/generator/USAGE +11 -0
  191. data/lib/generators/rails/generator/generator_generator.rb +25 -0
  192. data/lib/generators/rails/generator/templates/%file_name%_generator.rb.tt +5 -0
  193. data/lib/generators/rails/generator/templates/USAGE.tt +8 -0
  194. data/lib/generators/rails/generator/templates/templates/.empty_directory +0 -0
  195. data/lib/generators/rails/helper/USAGE +17 -0
  196. data/lib/generators/rails/helper/helper_generator.rb +13 -0
  197. data/lib/generators/rails/helper/templates/helper.rb +2 -0
  198. data/lib/generators/rails/integration_test/USAGE +10 -0
  199. data/lib/generators/rails/integration_test/integration_test_generator.rb +7 -0
  200. data/lib/generators/rails/mailer/USAGE +15 -0
  201. data/lib/generators/rails/mailer/mailer_generator.rb +14 -0
  202. data/lib/generators/rails/mailer/templates/mailer.rb +16 -0
  203. data/lib/generators/rails/metal/USAGE +8 -0
  204. data/lib/generators/rails/metal/metal_generator.rb +11 -0
  205. data/lib/generators/rails/metal/templates/metal.rb +12 -0
  206. data/lib/generators/rails/migration/USAGE +29 -0
  207. data/lib/generators/rails/migration/migration_generator.rb +8 -0
  208. data/lib/generators/rails/model/USAGE +30 -0
  209. data/lib/generators/rails/model/model_generator.rb +8 -0
  210. data/lib/generators/rails/model_subclass/model_subclass_generator.rb +12 -0
  211. data/lib/generators/rails/observer/USAGE +12 -0
  212. data/lib/generators/rails/observer/observer_generator.rb +7 -0
  213. data/lib/generators/rails/performance_test/USAGE +10 -0
  214. data/lib/generators/rails/performance_test/performance_test_generator.rb +7 -0
  215. data/lib/generators/rails/plugin/USAGE +13 -0
  216. data/lib/generators/rails/plugin/plugin_generator.rb +47 -0
  217. data/lib/generators/rails/plugin/templates/MIT-LICENSE.tt +20 -0
  218. data/lib/generators/rails/plugin/templates/README.tt +13 -0
  219. data/lib/generators/rails/plugin/templates/Rakefile.tt +10 -0
  220. data/lib/generators/rails/plugin/templates/init.rb +1 -0
  221. data/lib/generators/rails/plugin/templates/install.rb +1 -0
  222. data/lib/generators/rails/plugin/templates/lib/%file_name%.rb.tt +1 -0
  223. data/lib/generators/rails/plugin/templates/lib/tasks/%file_name%_tasks.rake.tt +4 -0
  224. data/lib/generators/rails/plugin/templates/uninstall.rb +1 -0
  225. data/lib/generators/rails/resource/USAGE +23 -0
  226. data/lib/generators/rails/resource/resource_generator.rb +34 -0
  227. data/lib/generators/rails/scaffold/USAGE +29 -0
  228. data/lib/generators/rails/scaffold/scaffold_generator.rb +13 -0
  229. data/lib/generators/rails/scaffold_controller/USAGE +20 -0
  230. data/lib/generators/rails/scaffold_controller/scaffold_controller_generator.rb +27 -0
  231. data/lib/generators/rails/scaffold_controller/templates/controller.rb +85 -0
  232. data/lib/generators/rails/session_migration/USAGE +8 -0
  233. data/lib/generators/rails/session_migration/session_migration_generator.rb +8 -0
  234. data/lib/generators/rails/stylesheets/USAGE +5 -0
  235. data/lib/generators/rails/stylesheets/stylesheets_generator.rb +9 -0
  236. data/lib/generators/rails/stylesheets/templates/scaffold.css +61 -0
  237. data/lib/generators/test_unit.rb +8 -0
  238. data/lib/generators/test_unit/controller/controller_generator.rb +14 -0
  239. data/lib/generators/test_unit/controller/templates/functional_test.rb +8 -0
  240. data/lib/generators/test_unit/helper/helper_generator.rb +13 -0
  241. data/lib/generators/test_unit/helper/templates/helper_test.rb +4 -0
  242. data/lib/generators/test_unit/integration/integration_generator.rb +13 -0
  243. data/lib/generators/test_unit/integration/templates/integration_test.rb +10 -0
  244. data/lib/generators/test_unit/mailer/mailer_generator.rb +21 -0
  245. data/lib/generators/test_unit/mailer/templates/fixture +3 -0
  246. data/lib/generators/test_unit/mailer/templates/functional_test.rb +22 -0
  247. data/lib/generators/test_unit/model/model_generator.rb +24 -0
  248. data/lib/generators/test_unit/model/templates/fixtures.yml +23 -0
  249. data/lib/generators/test_unit/model/templates/unit_test.rb +8 -0
  250. data/lib/generators/test_unit/observer/observer_generator.rb +13 -0
  251. data/lib/generators/test_unit/observer/templates/unit_test.rb +8 -0
  252. data/lib/generators/test_unit/performance/performance_generator.rb +13 -0
  253. data/lib/generators/test_unit/performance/templates/performance_test.rb +9 -0
  254. data/lib/generators/test_unit/plugin/plugin_generator.rb +13 -0
  255. data/lib/generators/test_unit/plugin/templates/%file_name%_test.rb.tt +8 -0
  256. data/lib/generators/test_unit/plugin/templates/test_helper.rb +3 -0
  257. data/lib/generators/test_unit/scaffold/scaffold_generator.rb +18 -0
  258. data/lib/generators/test_unit/scaffold/templates/functional_test.rb +47 -0
  259. data/lib/rails.rb +95 -0
  260. data/lib/rails/all.rb +14 -0
  261. data/lib/rails/application.rb +125 -0
  262. data/lib/rails/application/bootstrap.rb +85 -0
  263. data/lib/rails/application/configurable.rb +19 -0
  264. data/lib/rails/application/configuration.rb +86 -0
  265. data/lib/rails/application/finisher.rb +49 -0
  266. data/lib/rails/application/metal_loader.rb +50 -0
  267. data/lib/rails/application/railties.rb +31 -0
  268. data/lib/rails/application/routes_reloader.rb +46 -0
  269. data/lib/rails/backtrace_cleaner.rb +54 -0
  270. data/lib/rails/code_statistics.rb +107 -0
  271. data/lib/rails/commands.rb +72 -0
  272. data/lib/rails/commands/application.rb +13 -0
  273. data/lib/rails/commands/console.rb +55 -0
  274. data/lib/rails/commands/dbconsole.rb +104 -0
  275. data/lib/rails/commands/destroy.rb +9 -0
  276. data/lib/rails/commands/generate.rb +9 -0
  277. data/lib/rails/commands/performance/benchmarker.rb +23 -0
  278. data/lib/rails/commands/performance/profiler.rb +46 -0
  279. data/lib/rails/commands/plugin.rb +542 -0
  280. data/lib/rails/commands/runner.rb +51 -0
  281. data/lib/rails/commands/server.rb +75 -0
  282. data/lib/rails/commands/update.rb +9 -0
  283. data/lib/rails/configuration.rb +191 -0
  284. data/lib/rails/console/app.rb +33 -0
  285. data/lib/rails/console/helpers.rb +5 -0
  286. data/lib/rails/console/sandbox.rb +6 -0
  287. data/lib/rails/deprecation.rb +62 -0
  288. data/lib/rails/dispatcher.rb +24 -0
  289. data/lib/rails/engine.rb +130 -0
  290. data/lib/rails/engine/configurable.rb +25 -0
  291. data/lib/rails/engine/configuration.rb +49 -0
  292. data/lib/rails/generators.rb +289 -0
  293. data/lib/rails/generators/actions.rb +309 -0
  294. data/lib/rails/generators/active_model.rb +78 -0
  295. data/lib/rails/generators/base.rb +362 -0
  296. data/lib/rails/generators/generated_attribute.rb +48 -0
  297. data/lib/rails/generators/migration.rb +64 -0
  298. data/lib/rails/generators/named_base.rb +95 -0
  299. data/lib/rails/generators/resource_helpers.rb +80 -0
  300. data/lib/rails/generators/test_case.rb +239 -0
  301. data/lib/rails/initializable.rb +97 -0
  302. data/lib/rails/paths.rb +144 -0
  303. data/lib/rails/performance_test_help.rb +3 -0
  304. data/lib/rails/plugin.rb +64 -0
  305. data/lib/rails/rack.rb +8 -0
  306. data/lib/rails/rack/debugger.rb +23 -0
  307. data/lib/rails/rack/log_tailer.rb +33 -0
  308. data/lib/rails/rack/logger.rb +34 -0
  309. data/lib/rails/rack/static.rb +5 -0
  310. data/lib/rails/railtie.rb +74 -0
  311. data/lib/rails/railtie/configurable.rb +23 -0
  312. data/lib/rails/railtie/configuration.rb +9 -0
  313. data/lib/rails/railties_path.rb +1 -0
  314. data/lib/rails/ruby_version_check.rb +10 -0
  315. data/lib/rails/rubyprof_ext.rb +35 -0
  316. data/lib/rails/source_annotation_extractor.rb +102 -0
  317. data/lib/rails/subscriber.rb +108 -0
  318. data/lib/rails/subscriber/test_helper.rb +98 -0
  319. data/lib/rails/tasks.rb +16 -0
  320. data/lib/rails/tasks/annotations.rake +20 -0
  321. data/lib/rails/tasks/documentation.rake +96 -0
  322. data/lib/rails/tasks/framework.rake +69 -0
  323. data/lib/rails/tasks/log.rake +9 -0
  324. data/lib/rails/tasks/middleware.rake +7 -0
  325. data/lib/rails/tasks/misc.rake +71 -0
  326. data/lib/rails/tasks/routes.rake +17 -0
  327. data/lib/rails/tasks/statistics.rake +16 -0
  328. data/lib/rails/tasks/tmp.rake +37 -0
  329. data/lib/rails/test_help.rb +30 -0
  330. data/lib/rails/test_unit/railtie.rb +17 -0
  331. data/lib/rails/test_unit/testing.rake +130 -0
  332. data/lib/rails/version.rb +9 -0
  333. data/lib/rails/webrick_server.rb +156 -0
  334. metadata +427 -0
@@ -0,0 +1,222 @@
1
+ h2. Rails nested model forms
2
+
3
+ Creating a form for a model _and_ its associations can become quite tedious. Therefor Rails provides helpers to assist in dealing with the complexities of generating these forms _and_ the required CRUD operations to create, update, and destroy associations.
4
+
5
+ In this guide you will:
6
+
7
+ * do stuff
8
+
9
+ endprologue.
10
+
11
+ NOTE: This guide assumes the user knows how to use the "Rails form helpers":form_helpers.html in general. Also, it’s *not* an API reference. For a complete reference please visit "the Rails API documentation":http://api.rubyonrails.org/.
12
+
13
+
14
+ h3. Model setup
15
+
16
+ To be able to use the nested model functionality in your forms, the model will need to support some basic operations.
17
+
18
+ First of all, it needs to define a writer method for the attribute that corresponds to the association you are building a nested model form for. The +fields_for+ form helper will look for this method to decide whether or not a nested model form should be build.
19
+
20
+ If the associated object is an array a form builder will be yielded for each object, else only a single form builder will be yielded.
21
+
22
+ Consider a Person model with an associated Address. When asked to yield a nested FormBuilder for the +:address+ attribute, the +fields_for+ form helper will look for a method on the Person instance named +address_attributes=+.
23
+
24
+ h4. ActiveRecord::Base model
25
+
26
+ For an ActiveRecord::Base model and association this writer method is commonly defined with the +accepts_nested_attributes_for+ class method:
27
+
28
+ h5. has_one
29
+
30
+ <ruby>
31
+ class Person < ActiveRecord::Base
32
+ has_one :address
33
+ accepts_nested_attributes_for :address
34
+ end
35
+ </ruby>
36
+
37
+ h5. belongs_to
38
+
39
+ <ruby>
40
+ class Person < ActiveRecord::Base
41
+ belongs_to :firm
42
+ accepts_nested_attributes_for :firm
43
+ end
44
+ </ruby>
45
+
46
+ h5. has_many / has_and_belongs_to_many
47
+
48
+ <ruby>
49
+ class Person < ActiveRecord::Base
50
+ has_many :projects
51
+ accepts_nested_attributes_for :projects
52
+ end
53
+ </ruby>
54
+
55
+ h4. Custom model
56
+
57
+ As you might have inflected from this explanation, you _don’t_ necessarily need an ActiveRecord::Base model to use this functionality. The following examples are sufficient to enable the nested model form behaviour:
58
+
59
+ h5. Single associated object
60
+
61
+ <ruby>
62
+ class Person
63
+ def address
64
+ Address.new
65
+ end
66
+
67
+ def address_attributes=(attributes)
68
+ # ...
69
+ end
70
+ end
71
+ </ruby>
72
+
73
+ h5. Association collection
74
+
75
+ <ruby>
76
+ class Person
77
+ def projects
78
+ [Project.new, Project.new]
79
+ end
80
+
81
+ def projects_attributes=(attributes)
82
+ # ...
83
+ end
84
+ end
85
+ </ruby>
86
+
87
+ NOTE: See (TODO) in the advanced section for more information on how to deal with the CRUD operations in your custom model.
88
+
89
+ h3. Views
90
+
91
+ h4. Controller code
92
+
93
+ A nested model form will _only_ be build if the associated object(s) exist. This means that for a new model instance you would probably want to build the associated object(s) first.
94
+
95
+ Consider the following typical RESTful controller which will prepare a new Person instance and its +address+ and +projects+ associations before rendering the +new+ template:
96
+
97
+ <ruby>
98
+ class PeopleController < ActionController:Base
99
+ def new
100
+ @person = Person.new
101
+ @person.built_address
102
+ 2.times { @person.projects.build }
103
+ end
104
+
105
+ def create
106
+ @person = Person.new(params[:person])
107
+ if @person.save
108
+ # ...
109
+ end
110
+ end
111
+ end
112
+ </ruby>
113
+
114
+ NOTE: Obviously the instantiation of the associated object(s) can become tedious and not DRY, so you might want to move that into the model itself. ActiveRecord::Base provides an +after_initialize+ callback which is a good way to refactor this.
115
+
116
+ h4. Form code
117
+
118
+ Now that you have a model instance, with the appropriate methods and associated object(s), you can start building the nested model form.
119
+
120
+ h5. Standard form
121
+
122
+ Start out with a regular RESTful form:
123
+
124
+ <erb>
125
+ <% form_for @person do |f| %>
126
+ <%= f.text_field :name %>
127
+ <% end %>
128
+ </erb>
129
+
130
+ This will generate the following html:
131
+
132
+ <html>
133
+ <form action="/people" class="new_person" id="new_person" method="post">
134
+ <input id="person_name" name="person[name]" size="30" type="text" />
135
+ </form>
136
+ </html>
137
+
138
+ h5. Nested form for a single associated object
139
+
140
+ Now add a nested form for the +address+ association:
141
+
142
+ <erb>
143
+ <% form_for @person do |f| %>
144
+ <%= f.text_field :name %>
145
+
146
+ <% f.fields_for :address do |af| %>
147
+ <%= f.text_field :street %>
148
+ <% end %>
149
+ <% end %>
150
+ </erb>
151
+
152
+ This generates:
153
+
154
+ <html>
155
+ <form action="/people" class="new_person" id="new_person" method="post">
156
+ <input id="person_name" name="person[name]" size="30" type="text" />
157
+
158
+ <input id="person_address_attributes_street" name="person[address_attributes][street]" size="30" type="text" />
159
+ </form>
160
+ </html>
161
+
162
+ Notice that +fields_for+ recognized the +address+ as an association for which a nested model form should be build by the way it has namespaced the +name+ attribute.
163
+
164
+ When this form is posted the Rails parameter parser will construct a hash like the following:
165
+
166
+ <ruby>
167
+ {
168
+ "person" => {
169
+ "name" => "Eloy Duran",
170
+ "address_attributes" => {
171
+ "street" => "Nieuwe Prinsengracht"
172
+ }
173
+ }
174
+ }
175
+ </ruby>
176
+
177
+ That’s it. The controller will simply pass this hash on to the model from the +create+ action. The model will then handle building the +address+ association for you and automatically save it when the parent (+person+) is saved.
178
+
179
+ h5. Nested form for a collection of associated objects
180
+
181
+ The form code for an association collection is pretty similar to that of a single associated object:
182
+
183
+ <erb>
184
+ <% form_for @person do |f| %>
185
+ <%= f.text_field :name %>
186
+
187
+ <% f.fields_for :projects do |pf| %>
188
+ <%= f.text_field :name %>
189
+ <% end %>
190
+ <% end %>
191
+ </erb>
192
+
193
+ Which generates:
194
+
195
+ <html>
196
+ <form action="/people" class="new_person" id="new_person" method="post">
197
+ <input id="person_name" name="person[name]" size="30" type="text" />
198
+
199
+ <input id="person_projects_attributes_0_name" name="person[projects_attributes][0][name]" size="30" type="text" />
200
+ <input id="person_projects_attributes_1_name" name="person[projects_attributes][1][name]" size="30" type="text" />
201
+ </form>
202
+ </html>
203
+
204
+ As you can see it has generated 2 +project name+ inputs, one for each new +project+ that’s build in the controllers +new+ action. Only this time the +name+ attribute of the input contains a digit as an extra namespace. This will be parsed by the Rails parameter parser as:
205
+
206
+ <ruby>
207
+ {
208
+ "person" => {
209
+ "name" => "Eloy Duran",
210
+ "projects_attributes" => {
211
+ "0" => { "name" => "Project 1" },
212
+ "1" => { "name" => "Project 2" }
213
+ }
214
+ }
215
+ }
216
+ </ruby>
217
+
218
+ You can basically see the +projects_attributes+ hash as an array of attribute hashes. One for each model instance.
219
+
220
+ NOTE: The reason that +fields_for+ constructed a form which would result in a hash instead of an array is that it won't work for any forms nested deeper than one level deep.
221
+
222
+ TIP: You _can_ however pass an array to the writer method generated by +accepts_nested_attributes_for+ if you're using plain Ruby or some other API access. See (TODO) for more info and example.
@@ -0,0 +1,531 @@
1
+ h2. Performance Testing Rails Applications
2
+
3
+ This guide covers the various ways of performance testing a Ruby on Rails application. By referring to this guide, you will be able to:
4
+
5
+ * Understand the various types of benchmarking and profiling metrics
6
+ * Generate performance and benchmarking tests
7
+ * Use a GC-patched Ruby binary to measure memory usage and object allocation
8
+ * Understand the benchmarking information provided by Rails inside the log files
9
+ * Learn about various tools facilitating benchmarking and profiling
10
+
11
+ Performance testing is an integral part of the development cycle. It is very important that you don't make your end users wait for too long before the page is completely loaded. Ensuring a pleasant browsing experience for end users and cutting the cost of unnecessary hardware is important for any non-trivial web application.
12
+
13
+ endprologue.
14
+
15
+ h3. Performance Test Cases
16
+
17
+ Rails performance tests are a special type of integration tests, designed for benchmarking and profiling the test code. With performance tests, you can determine where your application's memory or speed problems are coming from, and get a more in-depth picture of those problems.
18
+
19
+ In a freshly generated Rails application, +test/performance/browsing_test.rb+ contains an example of a performance test:
20
+
21
+ <ruby>
22
+ require 'test_helper'
23
+ require 'performance_test_help'
24
+
25
+ # Profiling results for each test method are written to tmp/performance.
26
+ class BrowsingTest < ActionController::PerformanceTest
27
+ def test_homepage
28
+ get '/'
29
+ end
30
+ end
31
+ </ruby>
32
+
33
+ This example is a simple performance test case for profiling a GET request to the application's homepage.
34
+
35
+ h4. Generating Performance Tests
36
+
37
+ Rails provides a generator called +performance_test+ for creating new performance tests:
38
+
39
+ <shell>
40
+ script/generate performance_test homepage
41
+ </shell>
42
+
43
+ This generates +homepage_test.rb+ in the +test/performance+ directory:
44
+
45
+ <ruby>
46
+ require 'test_helper'
47
+ require 'performance_test_help'
48
+
49
+ class HomepageTest < ActionController::PerformanceTest
50
+ # Replace this with your real tests.
51
+ def test_homepage
52
+ get '/'
53
+ end
54
+ end
55
+ </ruby>
56
+
57
+ h4. Examples
58
+
59
+ Let's assume your application has the following controller and model:
60
+
61
+ <ruby>
62
+ # routes.rb
63
+ map.root :controller => 'home'
64
+ map.resources :posts
65
+
66
+ # home_controller.rb
67
+ class HomeController < ApplicationController
68
+ def dashboard
69
+ @users = User.last_ten(:include => :avatars)
70
+ @posts = Post.all_today
71
+ end
72
+ end
73
+
74
+ # posts_controller.rb
75
+ class PostsController < ApplicationController
76
+ def create
77
+ @post = Post.create(params[:post])
78
+ redirect_to(@post)
79
+ end
80
+ end
81
+
82
+ # post.rb
83
+ class Post < ActiveRecord::Base
84
+ before_save :recalculate_costly_stats
85
+
86
+ def slow_method
87
+ # I fire gallzilion queries sleeping all around
88
+ end
89
+
90
+ private
91
+
92
+ def recalculate_costly_stats
93
+ # CPU heavy calculations
94
+ end
95
+ end
96
+ </ruby>
97
+
98
+ h5. Controller Example
99
+
100
+ Because performance tests are a special kind of integration test, you can use the +get+ and +post+ methods in them.
101
+
102
+ Here's the performance test for +HomeController#dashboard+ and +PostsController#create+:
103
+
104
+ <ruby>
105
+ require 'test_helper'
106
+ require 'performance_test_help'
107
+
108
+ class PostPerformanceTest < ActionController::PerformanceTest
109
+ def setup
110
+ # Application requires logged-in user
111
+ login_as(:lifo)
112
+ end
113
+
114
+ def test_homepage
115
+ get '/dashboard'
116
+ end
117
+
118
+ def test_creating_new_post
119
+ post '/posts', :post => { :body => 'lifo is fooling you' }
120
+ end
121
+ end
122
+ </ruby>
123
+
124
+ You can find more details about the +get+ and +post+ methods in the "Testing Rails Applications":testing.html guide.
125
+
126
+ h5. Model Example
127
+
128
+ Even though the performance tests are integration tests and hence closer to the request/response cycle by nature, you can still performance test pure model code.
129
+
130
+ Performance test for +Post+ model:
131
+
132
+ <ruby>
133
+ require 'test_helper'
134
+ require 'performance_test_help'
135
+
136
+ class PostModelTest < ActionController::PerformanceTest
137
+ def test_creation
138
+ Post.create :body => 'still fooling you', :cost => '100'
139
+ end
140
+
141
+ def test_slow_method
142
+ # Using posts(:awesome) fixture
143
+ posts(:awesome).slow_method
144
+ end
145
+ end
146
+ </ruby>
147
+
148
+ h4. Modes
149
+
150
+ Performance tests can be run in two modes: Benchmarking and Profiling.
151
+
152
+ h5. Benchmarking
153
+
154
+ Benchmarking helps find out how fast each performance test runs. Each test case is run +4 times+ in benchmarking mode.
155
+
156
+ To run performance tests in benchmarking mode:
157
+
158
+ <shell>
159
+ $ rake test:benchmark
160
+ </shell>
161
+
162
+ h5. Profiling
163
+
164
+ Profiling helps you see the details of a performance test and provide an in-depth picture of the slow and memory hungry parts. Each test case is run +1 time+ in profiling mode.
165
+
166
+ To run performance tests in profiling mode:
167
+
168
+ <shell>
169
+ $ rake test:profile
170
+ </shell>
171
+
172
+ h4. Metrics
173
+
174
+ Benchmarking and profiling run performance tests in various modes described below.
175
+
176
+ h5. Wall Time
177
+
178
+ Wall time measures the real world time elapsed during the test run. It is affected by any other processes concurrently running on the system.
179
+
180
+ Mode: Benchmarking
181
+
182
+ h5. Process Time
183
+
184
+ Process time measures the time taken by the process. It is unaffected by any other processes running concurrently on the same system. Hence, process time is likely to be constant for any given performance test, irrespective of the machine load.
185
+
186
+ Mode: Profiling
187
+
188
+ h5. Memory
189
+
190
+ Memory measures the amount of memory used for the performance test case.
191
+
192
+ Mode: Benchmarking, Profiling "Requires GC Patched Ruby":#installing-gc-patched-ruby
193
+
194
+ h5. Objects
195
+
196
+ Objects measures the number of objects allocated for the performance test case.
197
+
198
+ Mode: Benchmarking, Profiling "Requires GC Patched Ruby":#installing-gc-patched-ruby
199
+
200
+ h5. GC Runs
201
+
202
+ GC Runs measures the number of times GC was invoked for the performance test case.
203
+
204
+ Mode: Benchmarking "Requires GC Patched Ruby":#installing-gc-patched-ruby
205
+
206
+ h5. GC Time
207
+
208
+ GC Time measures the amount of time spent in GC for the performance test case.
209
+
210
+ Mode: Benchmarking "Requires GC Patched Ruby":#installing-gc-patched-ruby
211
+
212
+ h4. Understanding the Output
213
+
214
+ Performance tests generate different outputs inside +tmp/performance+ directory depending on their mode and metric.
215
+
216
+ h5. Benchmarking
217
+
218
+ In benchmarking mode, performance tests generate two types of outputs:
219
+
220
+ h6. Command Line
221
+
222
+ This is the primary form of output in benchmarking mode. Example:
223
+
224
+ <shell>
225
+ BrowsingTest#test_homepage (31 ms warmup)
226
+ wall_time: 6 ms
227
+ memory: 437.27 KB
228
+ objects: 5514
229
+ gc_runs: 0
230
+ gc_time: 19 ms
231
+ </shell>
232
+
233
+ h6. CSV Files
234
+
235
+ Performance test results are also appended to +.csv+ files inside +tmp/performance+. For example, running the default +BrowsingTest#test_homepage+ will generate following five files:
236
+
237
+ * BrowsingTest#test_homepage_gc_runs.csv
238
+ * BrowsingTest#test_homepage_gc_time.csv
239
+ * BrowsingTest#test_homepage_memory.csv
240
+ * BrowsingTest#test_homepage_objects.csv
241
+ * BrowsingTest#test_homepage_wall_time.csv
242
+
243
+ As the results are appended to these files each time the performance tests are run in benchmarking mode, you can collect data over a period of time. This can be very helpful in analyzing the effects of code changes.
244
+
245
+ Sample output of +BrowsingTest#test_homepage_wall_time.csv+:
246
+
247
+ <shell>
248
+ measurement,created_at,app,rails,ruby,platform
249
+ 0.00738224999999992,2009-01-08T03:40:29Z,,2.3.0.master.0744148,ruby-1.8.6.110,i686-darwin9.0.0
250
+ 0.00755874999999984,2009-01-08T03:46:18Z,,2.3.0.master.0744148,ruby-1.8.6.110,i686-darwin9.0.0
251
+ 0.00762099999999993,2009-01-08T03:49:25Z,,2.3.0.master.0744148,ruby-1.8.6.110,i686-darwin9.0.0
252
+ 0.00603075000000008,2009-01-08T04:03:29Z,,2.3.0.master.0744148,ruby-1.8.6.111,i686-darwin9.1.0
253
+ 0.00619899999999995,2009-01-08T04:03:53Z,,2.3.0.master.0744148,ruby-1.8.6.111,i686-darwin9.1.0
254
+ 0.00755449999999991,2009-01-08T04:04:55Z,,2.3.0.master.0744148,ruby-1.8.6.110,i686-darwin9.0.0
255
+ 0.00595999999999997,2009-01-08T04:05:06Z,,2.3.0.master.0744148,ruby-1.8.6.111,i686-darwin9.1.0
256
+ 0.00740450000000004,2009-01-09T03:54:47Z,,2.3.0.master.859e150,ruby-1.8.6.110,i686-darwin9.0.0
257
+ 0.00603150000000008,2009-01-09T03:54:57Z,,2.3.0.master.859e150,ruby-1.8.6.111,i686-darwin9.1.0
258
+ 0.00771250000000012,2009-01-09T15:46:03Z,,2.3.0.master.859e150,ruby-1.8.6.110,i686-darwin9.0.0
259
+ </shell>
260
+
261
+ h5. Profiling
262
+
263
+ In profiling mode, you can choose from four types of output.
264
+
265
+ h6. Command Line
266
+
267
+ This is a very basic form of output in profiling mode:
268
+
269
+ <shell>
270
+ BrowsingTest#test_homepage (58 ms warmup)
271
+ process_time: 63 ms
272
+ memory: 832.13 KB
273
+ objects: 7882
274
+ </shell>
275
+
276
+ h6. Flat
277
+
278
+ Flat output shows the total amount of time spent in each method. "Check ruby prof documentation for a better explanation":http://ruby-prof.rubyforge.org/files/examples/flat_txt.html.
279
+
280
+ h6. Graph
281
+
282
+ Graph output shows how long each method takes to run, which methods call it and which methods it calls. "Check ruby prof documentation for a better explanation":http://ruby-prof.rubyforge.org/files/examples/graph_txt.html.
283
+
284
+ h6. Tree
285
+
286
+ Tree output is profiling information in calltree format for use by "kcachegrind":http://kcachegrind.sourceforge.net/html/Home.html and similar tools.
287
+
288
+ h4. Tuning Test Runs
289
+
290
+ By default, each performance test is run +4 times+ in benchmarking mode and +1 time+ in profiling. However, test runs can easily be configured.
291
+
292
+ WARNING: Performance test configurability is not yet enabled in Rails. But it will be soon.
293
+
294
+ h4. Performance Test Environment
295
+
296
+ Performance tests are run in the +test+ environment. But running performance tests will set the following configuration parameters:
297
+
298
+ <shell>
299
+ ActionController::Base.perform_caching = true
300
+ ActiveSupport::Dependencies.mechanism = :require
301
+ Rails.logger.level = ActiveSupport::BufferedLogger::INFO
302
+ </shell>
303
+
304
+ As +ActionController::Base.perform_caching+ is set to +true+, performance tests will behave much as they do in the +production+ environment.
305
+
306
+ h4. Installing GC-Patched Ruby
307
+
308
+ To get the best from Rails performance tests, you need to build a special Ruby binary with some super powers - "GC patch":http://rubyforge.org/tracker/download.php/1814/7062/17676/3291/ruby186gc.patch for measuring GC Runs/Time and memory/object allocation.
309
+
310
+ The process is fairly straightforward. If you've never compiled a Ruby binary before, follow these steps to build a ruby binary inside your home directory:
311
+
312
+ h5. Installation
313
+
314
+ Compile Ruby and apply this "GC Patch":http://rubyforge.org/tracker/download.php/1814/7062/17676/3291/ruby186gc.patch.
315
+
316
+ h5. Download and Extract
317
+
318
+ <shell>
319
+ [lifo@null ~]$ mkdir rubygc
320
+ [lifo@null ~]$ wget <download the latest stable ruby from ftp://ftp.ruby-lang.org/pub/ruby>
321
+ [lifo@null ~]$ tar -xzvf <ruby-version.tar.gz>
322
+ [lifo@null ~]$ cd <ruby-version>
323
+ </shell>
324
+
325
+ h5. Apply the Patch
326
+
327
+ <shell>
328
+ [lifo@null ruby-version]$ curl http://rubyforge.org/tracker/download.php/1814/7062/17676/3291/ruby186gc.patch | patch -p0
329
+ </shell>
330
+
331
+ h5. Configure and Install
332
+
333
+ The following will install ruby in your home directory's +/rubygc+ directory. Make sure to replace +<homedir>+ with a full patch to your actual home directory.
334
+
335
+ <shell>
336
+ [lifo@null ruby-version]$ ./configure --prefix=/<homedir>/rubygc
337
+ [lifo@null ruby-version]$ make && make install
338
+ </shell>
339
+
340
+ h5. Prepare Aliases
341
+
342
+ For convenience, add the following lines in your +~/.profile+:
343
+
344
+ <shell>
345
+ alias gcruby='~/rubygc/bin/ruby'
346
+ alias gcrake='~/rubygc/bin/rake'
347
+ alias gcgem='~/rubygc/bin/gem'
348
+ alias gcirb='~/rubygc/bin/irb'
349
+ alias gcrails='~/rubygc/bin/rails'
350
+ </shell>
351
+
352
+ h5. Install Rubygems and Dependency Gems
353
+
354
+ Download "Rubygems":http://rubyforge.org/projects/rubygems and install it from source. Rubygem's README file should have necessary installation instructions.
355
+
356
+ Additionally, install the following gems:
357
+
358
+ * +rake+
359
+ * +rails+
360
+ * +ruby-prof+
361
+ * +rack+
362
+ * +mysql+
363
+
364
+ If installing +mysql+ fails, you can try to install it manually:
365
+
366
+ <shell>
367
+ [lifo@null mysql]$ gcruby extconf.rb --with-mysql-config
368
+ [lifo@null mysql]$ make && make install
369
+ </shell>
370
+
371
+ And you're ready to go. Don't forget to use +gcruby+ and +gcrake+ aliases when running the performance tests.
372
+
373
+ h3. Command Line Tools
374
+
375
+ Writing performance test cases could be an overkill when you are looking for one time tests. Rails ships with two command line tools that enable quick and dirty performance testing:
376
+
377
+ h4. +benchmarker+
378
+
379
+ +benchmarker+ is a wrapper around Ruby's "Benchmark":http://ruby-doc.org/core/classes/Benchmark.html standard library.
380
+
381
+ Usage:
382
+
383
+ <shell>
384
+ $ script/performance/benchmarker [times] 'Person.expensive_way' 'Person.another_expensive_way' ...
385
+ </shell>
386
+
387
+ Examples:
388
+
389
+ <shell>
390
+ $ script/performance/benchmarker 10 'Item.all' 'CouchItem.all'
391
+ </shell>
392
+
393
+ If the +[times]+ argument is omitted, supplied methods are run just once:
394
+
395
+ <shell>
396
+ $ script/performance/benchmarker 'Item.first' 'Item.last'
397
+ </shell>
398
+
399
+ h4. +profiler+
400
+
401
+ +profiler+ is a wrapper around http://ruby-prof.rubyforge.org/[ruby-prof] gem.
402
+
403
+ Usage:
404
+
405
+ <shell>
406
+ $ script/performance/profiler 'Person.expensive_method(10)' [times] [flat|graph|graph_html]
407
+ </shell>
408
+
409
+ Examples:
410
+
411
+ <shell>
412
+ $ script/performance/profiler 'Item.all'
413
+ </shell>
414
+
415
+ This will profile +Item.all+ in +RubyProf::WALL_TIME+ measure mode. By default, it prints flat output to the shell.
416
+
417
+ <shell>
418
+ $ script/performance/profiler 'Item.all' 10 graph
419
+ </shell>
420
+
421
+ This will profile +10.times { Item.all }+ with +RubyProf::WALL_TIME+ measure mode and print graph output to the shell.
422
+
423
+ If you want to store the output in a file:
424
+
425
+ <shell>
426
+ $ script/performance/profiler 'Item.all' 10 graph 2> graph.txt
427
+ </shell>
428
+
429
+ h3. Helper Methods
430
+
431
+ Rails provides various helper methods inside Active Record, Action Controller and Action View to measure the time taken by a given piece of code. The method is called +benchmark()+ in all the three components.
432
+
433
+ h4. Model
434
+
435
+ <ruby>
436
+ Project.benchmark("Creating project") do
437
+ project = Project.create("name" => "stuff")
438
+ project.create_manager("name" => "David")
439
+ project.milestones << Milestone.find(:all)
440
+ end
441
+ </ruby>
442
+
443
+ This benchmarks the code enclosed in the +Project.benchmark("Creating project") do...end+ block and prints the result to the log file:
444
+
445
+ <ruby>
446
+ Creating project (185.3ms)
447
+ </ruby>
448
+
449
+ Please refer to the "API docs":http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M001336 for additional options to +benchmark()+
450
+
451
+ h4. Controller
452
+
453
+ Similarly, you could use this helper method inside "controllers":http://api.rubyonrails.org/classes/ActionController/Benchmarking/ClassMethods.html#M000715
454
+
455
+ <ruby>
456
+ def process_projects
457
+ self.class.benchmark("Processing projects") do
458
+ Project.process(params[:project_ids])
459
+ Project.update_cached_projects
460
+ end
461
+ end
462
+ </ruby>
463
+
464
+ NOTE: +benchmark+ is a class method inside controllers
465
+
466
+ h4. View
467
+
468
+ And in "views":http://api.rubyonrails.org/classes/ActionController/Benchmarking/ClassMethods.html#M000715:
469
+
470
+ <erb>
471
+ <% benchmark("Showing projects partial") do %>
472
+ <%= render :partial => @projects %>
473
+ <% end %>
474
+ </erb>
475
+
476
+ h3. Request Logging
477
+
478
+ Rails log files contain very useful information about the time taken to serve each request. Here's a typical log file entry:
479
+
480
+ <shell>
481
+ Processing ItemsController#index (for 127.0.0.1 at 2009-01-08 03:06:39) [GET]
482
+ Rendering template within layouts/items
483
+ Rendering items/index
484
+ Completed in 5ms (View: 2, DB: 0) | 200 OK [http://0.0.0.0/items]
485
+ </shell>
486
+
487
+ For this section, we're only interested in the last line:
488
+
489
+ <shell>
490
+ Completed in 5ms (View: 2, DB: 0) | 200 OK [http://0.0.0.0/items]
491
+ </shell>
492
+
493
+ This data is fairly straightforward to understand. Rails uses millisecond(ms) as the metric to measure the time taken. The complete request spent 5 ms inside Rails, out of which 2 ms were spent rendering views and none was spent communication with the database. It's safe to assume that the remaining 3 ms were spent inside the controller.
494
+
495
+ Michael Koziarski has an "interesting blog post":http://www.therailsway.com/2009/1/6/requests-per-second explaining the importance of using milliseconds as the metric.
496
+
497
+ h3. Useful Links
498
+
499
+ h4. Rails Plugins and Gems
500
+
501
+ * "Rails Analyzer":http://rails-analyzer.rubyforge.org
502
+ * "Palmist":http://www.flyingmachinestudios.com/projects/
503
+ * "Rails Footnotes":http://github.com/josevalim/rails-footnotes/tree/master
504
+ * "Query Reviewer":http://github.com/dsboulder/query_reviewer/tree/master
505
+
506
+ h4. Generic Tools
507
+
508
+ * "httperf":http://www.hpl.hp.com/research/linux/httperf/
509
+ * "ab":http://httpd.apache.org/docs/2.2/programs/ab.html
510
+ * "JMeter":http://jakarta.apache.org/jmeter/
511
+ * "kcachegrind":http://kcachegrind.sourceforge.net/html/Home.html
512
+
513
+ h4. Tutorials and Documentation
514
+
515
+ * "ruby-prof API Documentation":http://ruby-prof.rubyforge.org
516
+ * "Request Profiling Railscast":http://railscasts.com/episodes/98-request-profiling - Outdated, but useful for understanding call graphs
517
+
518
+ h3. Commercial Products
519
+
520
+ Rails has been lucky to have three startups dedicated to Rails specific performance tools:
521
+
522
+ * "New Relic":http://www.newrelic.com
523
+ * "Fiveruns":http://www.fiveruns.com
524
+ * "Scout":http://scoutapp.com
525
+
526
+ h3. Changelog
527
+
528
+ "Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/4
529
+
530
+ * January 9, 2009: Complete rewrite by "Pratik":credits.html#lifo
531
+ * September 6, 2008: Initial version by Matthew Bergman