CINXE.COM

Ruby on Rails 3.1 Release Notes — Ruby on Rails Guides

<!doctype html> <html dir="ltr" lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Ruby on Rails 3.1 Release Notes — Ruby on Rails Guides</title> <link rel="stylesheet" type="text/css" href="stylesheets/style-6fc5bf25e695f363fd1dd3d9dbb2f997.css" data-turbo-track="reload"> <link rel="stylesheet" type="text/css" href="stylesheets/print-a87ee66d50ce96bb83ac082f1249fe3e.css" media="print"> <link rel="stylesheet" type="text/css" href="stylesheets/highlight-2794201d063bd2e4dbd0f0874c2a3f6f.css" data-turbo-track="reload"> <link rel="icon" href="images/favicon.ico" sizes="any"> <link rel="apple-touch-icon" href="images/icon.png"> <link rel="canonical" href="https://guides.rubyonrails.org/3_1_release_notes.html"> <script src="javascripts/@hotwired--turbo-764f59c7edbeb902a9068c0340dd274e.js" data-turbo-track="reload"></script> <script src="javascripts/clipboard-8b7aed6f069f0cf58eeae353cd2f898b.js" data-turbo-track="reload"></script> <script src="javascripts/guides-751b87e159daf790ddf7e8e88ad8465a.js" data-turbo-track="reload"></script> <meta property="og:title" content="Ruby on Rails 3.1 Release Notes — Ruby on Rails Guides" /> <meta name="description" content="Ruby on Rails 3.1 Release NotesHighlights in Rails 3.1: Streaming Reversible Migrations Assets Pipeline jQuery as the default JavaScript library These release notes cover only the major changes. To learn about various bug fixes and changes, please refer to the changelogs or check out the list of commits in the main Rails repository on GitHub." /> <meta property="og:description" content="Ruby on Rails 3.1 Release NotesHighlights in Rails 3.1: Streaming Reversible Migrations Assets Pipeline jQuery as the default JavaScript library These release notes cover only the major changes. To learn about various bug fixes and changes, please refer to the changelogs or check out the list of commits in the main Rails repository on GitHub." /> <meta property="og:locale" content="en_US" /> <meta property="og:site_name" content="Ruby on Rails Guides" /> <meta property="og:image" content="https://avatars.githubusercontent.com/u/4223" /> <meta property="og:type" content="website" /> <link rel="preconnect" href="https://fonts.googleapis.com"> <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> <link href="https://fonts.googleapis.com/css2?family=Noto+Sans+Arabic:wght@100..900&display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=Heebo:wght@100..900&family=Noto+Sans+Arabic:wght@100..900&display=swap" rel="stylesheet"> <meta name="theme-color" content="#C81418"> </head> <body dir="ltr" class="guide no-js"> <script> document.body.classList.remove('no-js') </script> <a id="main-skip-link" href="#main" class="skip-link" data-turbo="false"> Skip to main content </a> <div id="mobile-navigation-bar"> <div class="wrapper"> <strong class="more-info-label">More at <a href="https://rubyonrails.org/">rubyonrails.org:</a> </strong> <button type="button" class="js-only red-button more-info-button" id="more-info" aria-controls="more-info-links" aria-expanded="false"> More Ruby on Rails </button> <ul id="more-info-links" class="more-info-links hidden"> <li class="more-info"><a href="https://rubyonrails.org/blog">Blog</a></li> <li class="more-info"><a href="https://guides.rubyonrails.org/">Guides</a></li> <li class="more-info"><a href="https://api.rubyonrails.org/">API</a></li> <li class="more-info"><a href="https://discuss.rubyonrails.org/">Forum</a></li> <li class="more-info"><a href="https://github.com/rails/rails">Contribute on GitHub</a></li> </ul> </div> </div> <header id="page-header"> <div class="wrapper clearfix"> <nav id="feature-nav"> <div class="header-logo"> <a href="index.html" title="Guides home for v8.0.1 Guides">Guides</a> <span id="version-switcher" class="js-only"> <label for="version-switcher-select">Version: <span class="visibly-hidden">pick from the list to go to that Rails version's guides</span></label> <select id="version-switcher-select" class="guides-version"> <option value="https://edgeguides.rubyonrails.org/">Edge</option> <option value="https://guides.rubyonrails.org/v8.0/3_1_release_notes.html" selected>8.0</option> <option value="https://guides.rubyonrails.org/v7.2/3_1_release_notes.html">7.2</option> <option value="https://guides.rubyonrails.org/v7.1/3_1_release_notes.html">7.1</option> <option value="https://guides.rubyonrails.org/v7.0/3_1_release_notes.html">7.0</option> <option value="https://guides.rubyonrails.org/v6.1/3_1_release_notes.html">6.1</option> <option value="https://guides.rubyonrails.org/v6.0/3_1_release_notes.html">6.0</option> <option value="https://guides.rubyonrails.org/v5.2/3_1_release_notes.html">5.2</option> <option value="https://guides.rubyonrails.org/v5.1/3_1_release_notes.html">5.1</option> <option value="https://guides.rubyonrails.org/v5.0/3_1_release_notes.html">5.0</option> <option value="https://guides.rubyonrails.org/v4.2/3_1_release_notes.html">4.2</option> <option value="https://guides.rubyonrails.org/v4.1/3_1_release_notes.html">4.1</option> <option value="https://guides.rubyonrails.org/v4.0/3_1_release_notes.html">4.0</option> <option value="https://guides.rubyonrails.org/v3.2/3_1_release_notes.html">3.2</option> <option value="https://guides.rubyonrails.org/v3.1/3_1_release_notes.html">3.1</option> <option value="https://guides.rubyonrails.org/v3.0/3_1_release_notes.html">3.0</option> <option value="https://guides.rubyonrails.org/v2.3/3_1_release_notes.html">2.3</option> </select> </span> </div> <ul class="nav"> <li><a class="nav-item" id="home_nav" href="https://rubyonrails.org/">Home</a></li> <li class="guides-index guides-index-large"> <a href="index.html" id="guides-menu-button" data-aria-controls="guides" data-aria-expanded="false" class="guides-index-item nav-item">Guides Index</a> <div id="guides" class="clearfix" style="display: none;"> <hr /> <dl class="guides-section-container"> <div class="guides-section"> <dt>Start Here</dt> <dd><a href="getting_started.html">Getting Started with Rails</a></dd> <dd><a href="install_ruby_on_rails.html">Install Ruby on Rails</a></dd> </div> <div class="guides-section"> <dt>Models</dt> <dd><a href="active_record_basics.html">Active Record Basics</a></dd> <dd><a href="active_record_migrations.html">Active Record Migrations</a></dd> <dd><a href="active_record_validations.html">Active Record Validations</a></dd> <dd><a href="active_record_callbacks.html">Active Record Callbacks</a></dd> <dd><a href="association_basics.html">Active Record Associations</a></dd> <dd><a href="active_record_querying.html">Active Record Query Interface</a></dd> <dd><a href="active_model_basics.html">Active Model Basics</a></dd> </div> <div class="guides-section"> <dt>Views</dt> <dd><a href="action_view_overview.html">Action View Overview</a></dd> <dd><a href="layouts_and_rendering.html">Layouts and Rendering in Rails</a></dd> <dd><a href="action_view_helpers.html">Action View Helpers</a></dd> <dd><a href="form_helpers.html">Action View Form Helpers</a></dd> </div> <div class="guides-section"> <dt>Controllers</dt> <dd><a href="action_controller_overview.html">Action Controller Overview</a></dd> <dd><a href="action_controller_advanced_topics.html">Action Controller Advanced Topics</a></dd> <dd><a href="routing.html">Rails Routing from the Outside In</a></dd> </div> <div class="guides-section"> <dt>Other Components</dt> <dd><a href="active_support_core_extensions.html">Active Support Core Extensions</a></dd> <dd><a href="action_mailer_basics.html">Action Mailer Basics</a></dd> <dd><a href="action_mailbox_basics.html">Action Mailbox Basics</a></dd> <dd><a href="action_text_overview.html">Action Text Overview</a></dd> <dd><a href="active_job_basics.html">Active Job Basics</a></dd> <dd><a href="active_storage_overview.html">Active Storage Overview</a></dd> <dd><a href="action_cable_overview.html">Action Cable Overview</a></dd> </div> <div class="guides-section"> <dt>Digging Deeper</dt> <dd><a href="i18n.html">Rails Internationalization (I18n) API</a></dd> <dd><a href="testing.html">Testing Rails Applications</a></dd> <dd><a href="debugging_rails_applications.html">Debugging Rails Applications</a></dd> <dd><a href="configuring.html">Configuring Rails Applications</a></dd> <dd><a href="command_line.html">The Rails Command Line</a></dd> <dd><a href="asset_pipeline.html">The Asset Pipeline</a></dd> <dd><a href="working_with_javascript_in_rails.html">Working with JavaScript in Rails</a></dd> <dd><a href="autoloading_and_reloading_constants.html">Autoloading and Reloading</a></dd> <dd><a href="api_app.html">Using Rails for API-only Applications</a></dd> </div> <div class="guides-section"> <dt>Going to Production</dt> <dd><a href="tuning_performance_for_deployment.html">Tuning Performance for Deployment</a></dd> <dd><a href="caching_with_rails.html">Caching with Rails: An Overview</a></dd> <dd><a href="security.html">Securing Rails Applications</a></dd> <dd><a href="error_reporting.html">Error Reporting in Rails Applications</a></dd> </div> <div class="guides-section"> <dt>Advanced Active Record</dt> <dd><a href="active_record_multiple_databases.html">Multiple Databases</a></dd> <dd><a href="active_record_composite_primary_keys.html">Composite Primary Keys</a></dd> </div> <div class="guides-section"> <dt>Extending Rails</dt> <dd><a href="rails_on_rack.html">Rails on Rack</a></dd> <dd><a href="generators.html">Creating and Customizing Rails Generators &amp; Templates</a></dd> </div> <div class="guides-section"> <dt>Contributing</dt> <dd><a href="contributing_to_ruby_on_rails.html">Contributing to Ruby on Rails</a></dd> <dd><a href="api_documentation_guidelines.html">API Documentation Guidelines</a></dd> <dd><a href="ruby_on_rails_guides_guidelines.html">Guides Guidelines</a></dd> <dd><a href="development_dependencies_install.html">Installing Rails Core Development Dependencies</a></dd> </div> <div class="guides-section"> <dt>Policies</dt> <dd><a href="maintenance_policy.html">Maintenance Policy</a></dd> </div> <div class="guides-section"> <dt>Release Notes</dt> <dd><a href="upgrading_ruby_on_rails.html">Upgrading Ruby on Rails</a></dd> <dd><a href="8_0_release_notes.html">Version 8.0 - November 2024</a></dd> <dd><a href="7_2_release_notes.html">Version 7.2 - August 2024</a></dd> <dd><a href="7_1_release_notes.html">Version 7.1 - October 2023</a></dd> <dd><a href="7_0_release_notes.html">Version 7.0 - December 2021</a></dd> <dd><a href="6_1_release_notes.html">Version 6.1 - December 2020</a></dd> <dd><a href="6_0_release_notes.html">Version 6.0 - August 2019</a></dd> <dd><a href="5_2_release_notes.html">Version 5.2 - April 2018</a></dd> <dd><a href="5_1_release_notes.html">Version 5.1 - April 2017</a></dd> <dd><a href="5_0_release_notes.html">Version 5.0 - June 2016</a></dd> <dd><a href="4_2_release_notes.html">Version 4.2 - December 2014</a></dd> <dd><a href="4_1_release_notes.html">Version 4.1 - April 2014</a></dd> <dd><a href="4_0_release_notes.html">Version 4.0 - June 2013</a></dd> <dd><a href="3_2_release_notes.html">Version 3.2 - January 2012</a></dd> <dd><a href="3_1_release_notes.html">Version 3.1 - August 2011</a></dd> <dd><a href="3_0_release_notes.html">Version 3.0 - August 2010</a></dd> <dd><a href="2_3_release_notes.html">Version 2.3 - March 2009</a></dd> <dd><a href="2_2_release_notes.html">Version 2.2 - November 2008</a></dd> </div> </dl> </div> </li> <li><a class="nav-item" href="contributing_to_ruby_on_rails.html">Contribute</a></li> <li class="guides-index guides-index-small js-only"> <label for="guides-selector"> Navigate to a guide: </label> <select id="guides-selector" class="guides-index-item nav-item"> <option value="index.html">Guides Index</option> <optgroup label="Start Here"> <option value="getting_started.html">Getting Started with Rails</option> <option value="install_ruby_on_rails.html">Install Ruby on Rails</option> </optgroup> <optgroup label="Models"> <option value="active_record_basics.html">Active Record Basics</option> <option value="active_record_migrations.html">Active Record Migrations</option> <option value="active_record_validations.html">Active Record Validations</option> <option value="active_record_callbacks.html">Active Record Callbacks</option> <option value="association_basics.html">Active Record Associations</option> <option value="active_record_querying.html">Active Record Query Interface</option> <option value="active_model_basics.html">Active Model Basics</option> </optgroup> <optgroup label="Views"> <option value="action_view_overview.html">Action View Overview</option> <option value="layouts_and_rendering.html">Layouts and Rendering in Rails</option> <option value="action_view_helpers.html">Action View Helpers</option> <option value="form_helpers.html">Action View Form Helpers</option> </optgroup> <optgroup label="Controllers"> <option value="action_controller_overview.html">Action Controller Overview</option> <option value="action_controller_advanced_topics.html">Action Controller Advanced Topics</option> <option value="routing.html">Rails Routing from the Outside In</option> </optgroup> <optgroup label="Other Components"> <option value="active_support_core_extensions.html">Active Support Core Extensions</option> <option value="action_mailer_basics.html">Action Mailer Basics</option> <option value="action_mailbox_basics.html">Action Mailbox Basics</option> <option value="action_text_overview.html">Action Text Overview</option> <option value="active_job_basics.html">Active Job Basics</option> <option value="active_storage_overview.html">Active Storage Overview</option> <option value="action_cable_overview.html">Action Cable Overview</option> </optgroup> <optgroup label="Digging Deeper"> <option value="i18n.html">Rails Internationalization (I18n) API</option> <option value="testing.html">Testing Rails Applications</option> <option value="debugging_rails_applications.html">Debugging Rails Applications</option> <option value="configuring.html">Configuring Rails Applications</option> <option value="command_line.html">The Rails Command Line</option> <option value="asset_pipeline.html">The Asset Pipeline</option> <option value="working_with_javascript_in_rails.html">Working with JavaScript in Rails</option> <option value="autoloading_and_reloading_constants.html">Autoloading and Reloading</option> <option value="api_app.html">Using Rails for API-only Applications</option> </optgroup> <optgroup label="Going to Production"> <option value="tuning_performance_for_deployment.html">Tuning Performance for Deployment</option> <option value="caching_with_rails.html">Caching with Rails: An Overview</option> <option value="security.html">Securing Rails Applications</option> <option value="error_reporting.html">Error Reporting in Rails Applications</option> </optgroup> <optgroup label="Advanced Active Record"> <option value="active_record_multiple_databases.html">Multiple Databases</option> <option value="active_record_composite_primary_keys.html">Composite Primary Keys</option> </optgroup> <optgroup label="Extending Rails"> <option value="rails_on_rack.html">Rails on Rack</option> <option value="generators.html">Creating and Customizing Rails Generators &amp; Templates</option> </optgroup> <optgroup label="Contributing"> <option value="contributing_to_ruby_on_rails.html">Contributing to Ruby on Rails</option> <option value="api_documentation_guidelines.html">API Documentation Guidelines</option> <option value="ruby_on_rails_guides_guidelines.html">Guides Guidelines</option> <option value="development_dependencies_install.html">Installing Rails Core Development Dependencies</option> </optgroup> <optgroup label="Policies"> <option value="maintenance_policy.html">Maintenance Policy</option> </optgroup> <optgroup label="Release Notes"> <option value="upgrading_ruby_on_rails.html">Upgrading Ruby on Rails</option> <option value="8_0_release_notes.html">Version 8.0 - November 2024</option> <option value="7_2_release_notes.html">Version 7.2 - August 2024</option> <option value="7_1_release_notes.html">Version 7.1 - October 2023</option> <option value="7_0_release_notes.html">Version 7.0 - December 2021</option> <option value="6_1_release_notes.html">Version 6.1 - December 2020</option> <option value="6_0_release_notes.html">Version 6.0 - August 2019</option> <option value="5_2_release_notes.html">Version 5.2 - April 2018</option> <option value="5_1_release_notes.html">Version 5.1 - April 2017</option> <option value="5_0_release_notes.html">Version 5.0 - June 2016</option> <option value="4_2_release_notes.html">Version 4.2 - December 2014</option> <option value="4_1_release_notes.html">Version 4.1 - April 2014</option> <option value="4_0_release_notes.html">Version 4.0 - June 2013</option> <option value="3_2_release_notes.html">Version 3.2 - January 2012</option> <option value="3_1_release_notes.html">Version 3.1 - August 2011</option> <option value="3_0_release_notes.html">Version 3.0 - August 2010</option> <option value="2_3_release_notes.html">Version 2.3 - March 2009</option> <option value="2_2_release_notes.html">Version 2.2 - November 2008</option> </optgroup> </select> </li> </ul> </nav> </div> </header> <hr class="hide" /> <main id="main"> <article> <header id="feature"> <div class="wrapper"> <h1>Ruby on Rails 3.1 Release Notes</h1><p>Highlights in Rails 3.1:</p> <ul> <li>Streaming</li> <li>Reversible Migrations</li> <li>Assets Pipeline</li> <li>jQuery as the default JavaScript library</li> </ul> <p>These release notes cover only the major changes. To learn about various bug fixes and changes, please refer to the changelogs or check out the <a href="https://github.com/rails/rails/commits/3-1-stable">list of commits</a> in the main Rails repository on GitHub.</p> <nav id="column-side" aria-label="Chapter" class="guide-index" data-turbo="false"> <a id="chapter-nav-skip-link" href="#article-body" class="skip-link"> Skip to article body </a> <h2 class="chapter"> <picture aria-hidden="true"> <!-- Using the `source` HTML tag to set the dark theme image --> <source srcset="images/icon_book-close-bookmark-1-wht.svg" media="(prefers-color-scheme: dark)" /> <img src="images/icon_book-close-bookmark-1.svg" alt="Chapter Icon" /> </picture> Chapters </h2> <ol class="chapters"> <li><a href="#upgrading-to-rails-3-1">Upgrading to Rails 3.1</a> <ul> <li><a href="#rails-3-1-requires-at-least-ruby-1-8-7">Rails 3.1 requires at least Ruby 1.8.7</a></li> <li><a href="#what-to-update-in-your-apps">What to update in your apps</a></li> </ul></li> <li><a href="#creating-a-rails-3-1-application">Creating a Rails 3.1 application</a> <ul> <li><a href="#vendoring-gems">Vendoring Gems</a></li> <li><a href="#living-on-the-edge">Living on the Edge</a></li> </ul></li> <li><a href="#rails-architectural-changes">Rails Architectural Changes</a> <ul> <li><a href="#assets-pipeline">Assets Pipeline</a></li> <li><a href="#http-streaming">HTTP Streaming</a></li> <li><a href="#default-js-library-is-now-jquery">Default JS library is now jQuery</a></li> <li><a href="#identity-map">Identity Map</a></li> </ul></li> <li><a href="#railties">Railties</a></li> <li><a href="#action-pack">Action Pack</a> <ul> <li><a href="#action-controller">Action Controller</a></li> <li><a href="#action-dispatch">Action Dispatch</a></li> <li><a href="#action-view">Action View</a></li> </ul></li> <li><a href="#active-record">Active Record</a></li> <li><a href="#active-model">Active Model</a></li> <li><a href="#active-resource">Active Resource</a></li> <li><a href="#active-support">Active Support</a></li> <li><a href="#credits">Credits</a></li> </ol> </nav> </div> </header> <div class="wrapper"> <div id="column-main"> <section id="article-body"> <h2 id="upgrading-to-rails-3-1"><a class="anchorlink" href="#upgrading-to-rails-3-1" data-turbo="false"><span>1</span> Upgrading to Rails 3.1</a></h2><p>If you're upgrading an existing application, it's a great idea to have good test coverage before going in. You should also first upgrade to Rails 3 in case you haven't and make sure your application still runs as expected before attempting to update to Rails 3.1. Then take heed of the following changes:</p><h3 id="rails-3-1-requires-at-least-ruby-1-8-7"><a class="anchorlink" href="#rails-3-1-requires-at-least-ruby-1-8-7" data-turbo="false"><span>1.1</span> Rails 3.1 requires at least Ruby 1.8.7</a></h3><p>Rails 3.1 requires Ruby 1.8.7 or higher. Support for all of the previous Ruby versions has been dropped officially and you should upgrade as early as possible. Rails 3.1 is also compatible with Ruby 1.9.2.</p><div class="interstitial info"><p>Note that Ruby 1.8.7 p248 and p249 have marshalling bugs that crash Rails. Ruby Enterprise Edition have these fixed since release 1.8.7-2010.02 though. On the 1.9 front, Ruby 1.9.1 is not usable because it outright segfaults, so if you want to use 1.9.x jump on 1.9.2 for smooth sailing.</p></div><h3 id="what-to-update-in-your-apps"><a class="anchorlink" href="#what-to-update-in-your-apps" data-turbo="false"><span>1.2</span> What to update in your apps</a></h3><p>The following changes are meant for upgrading your application to Rails 3.1.3, the latest 3.1.x version of Rails.</p><h4 id="gemfile"><a class="anchorlink" href="#gemfile" data-turbo="false"><span>1.2.1</span> Gemfile</a></h4><p>Make the following changes to your <code>Gemfile</code>.</p><div class="interstitial code"> <pre><code class="highlight ruby"><span class="n">gem</span> <span class="s2">"rails"</span><span class="p">,</span> <span class="s2">"= 3.1.3"</span> <span class="n">gem</span> <span class="s2">"mysql2"</span> <span class="c1"># Needed for the new asset pipeline</span> <span class="n">group</span> <span class="ss">:assets</span> <span class="k">do</span> <span class="n">gem</span> <span class="s2">"sass-rails"</span><span class="p">,</span> <span class="s2">"~&gt; 3.1.5"</span> <span class="n">gem</span> <span class="s2">"coffee-rails"</span><span class="p">,</span> <span class="s2">"~&gt; 3.1.1"</span> <span class="n">gem</span> <span class="s2">"uglifier"</span><span class="p">,</span> <span class="s2">"&gt;= 1.0.3"</span> <span class="k">end</span> <span class="c1"># jQuery is the default JavaScript library in Rails 3.1</span> <span class="n">gem</span> <span class="s2">"jquery-rails"</span> </code></pre> <button class="clipboard-button" data-clipboard-text="gem &quot;rails&quot;, &quot;= 3.1.3&quot; gem &quot;mysql2&quot; # Needed for the new asset pipeline group :assets do gem &quot;sass-rails&quot;, &quot;~> 3.1.5&quot; gem &quot;coffee-rails&quot;, &quot;~> 3.1.1&quot; gem &quot;uglifier&quot;, &quot;>= 1.0.3&quot; end # jQuery is the default JavaScript library in Rails 3.1 gem &quot;jquery-rails&quot; ">Copy</button> </div> <h4 id="config-application-rb"><a class="anchorlink" href="#config-application-rb" data-turbo="false"><span>1.2.2</span> config/application.rb</a></h4> <ul> <li><p>The asset pipeline requires the following additions:</p><div class="interstitial code"> <pre><code class="highlight ruby"><span class="n">config</span><span class="p">.</span><span class="nf">assets</span><span class="p">.</span><span class="nf">enabled</span> <span class="o">=</span> <span class="kp">true</span> <span class="n">config</span><span class="p">.</span><span class="nf">assets</span><span class="p">.</span><span class="nf">version</span> <span class="o">=</span> <span class="s1">'1.0'</span> </code></pre> <button class="clipboard-button" data-clipboard-text="config.assets.enabled = true config.assets.version = '1.0' ">Copy</button> </div></li> <li><p>If your application is using the "/assets" route for a resource you may want change the prefix used for assets to avoid conflicts:</p><div class="interstitial code"> <pre><code class="highlight ruby"><span class="c1"># Defaults to '/assets'</span> <span class="n">config</span><span class="p">.</span><span class="nf">assets</span><span class="p">.</span><span class="nf">prefix</span> <span class="o">=</span> <span class="s1">'/asset-files'</span> </code></pre> <button class="clipboard-button" data-clipboard-text="# Defaults to '/assets' config.assets.prefix = '/asset-files' ">Copy</button> </div></li> </ul> <h4 id="config-environments-development-rb"><a class="anchorlink" href="#config-environments-development-rb" data-turbo="false"><span>1.2.3</span> config/environments/development.rb</a></h4> <ul> <li><p>Remove the RJS setting <code>config.action_view.debug_rjs = true</code>.</p></li> <li><p>Add the following, if you enable the asset pipeline.</p><div class="interstitial code"> <pre><code class="highlight ruby"><span class="c1"># Do not compress assets</span> <span class="n">config</span><span class="p">.</span><span class="nf">assets</span><span class="p">.</span><span class="nf">compress</span> <span class="o">=</span> <span class="kp">false</span> <span class="c1"># Expands the lines which load the assets</span> <span class="n">config</span><span class="p">.</span><span class="nf">assets</span><span class="p">.</span><span class="nf">debug</span> <span class="o">=</span> <span class="kp">true</span> </code></pre> <button class="clipboard-button" data-clipboard-text="# Do not compress assets config.assets.compress = false # Expands the lines which load the assets config.assets.debug = true ">Copy</button> </div></li> </ul> <h4 id="config-environments-production-rb"><a class="anchorlink" href="#config-environments-production-rb" data-turbo="false"><span>1.2.4</span> config/environments/production.rb</a></h4> <ul> <li><p>Again, most of the changes below are for the asset pipeline. You can read more about these in the <a href="asset_pipeline.html">Asset Pipeline</a> guide.</p><div class="interstitial code"> <pre><code class="highlight ruby"><span class="c1"># Compress JavaScripts and CSS</span> <span class="n">config</span><span class="p">.</span><span class="nf">assets</span><span class="p">.</span><span class="nf">compress</span> <span class="o">=</span> <span class="kp">true</span> <span class="c1"># Don't fallback to assets pipeline if a precompiled asset is missed</span> <span class="n">config</span><span class="p">.</span><span class="nf">assets</span><span class="p">.</span><span class="nf">compile</span> <span class="o">=</span> <span class="kp">false</span> <span class="c1"># Generate digests for assets URLs</span> <span class="n">config</span><span class="p">.</span><span class="nf">assets</span><span class="p">.</span><span class="nf">digest</span> <span class="o">=</span> <span class="kp">true</span> <span class="c1"># Defaults to Rails.root.join("public/assets")</span> <span class="c1"># config.assets.manifest = YOUR_PATH</span> <span class="c1"># Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)</span> <span class="c1"># config.assets.precompile `= %w( admin.js admin.css )</span> <span class="c1"># Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.</span> <span class="c1"># config.force_ssl = true</span> </code></pre> <button class="clipboard-button" data-clipboard-text="# Compress JavaScripts and CSS config.assets.compress = true # Don't fallback to assets pipeline if a precompiled asset is missed config.assets.compile = false # Generate digests for assets URLs config.assets.digest = true # Defaults to Rails.root.join(&quot;public/assets&quot;) # config.assets.manifest = YOUR_PATH # Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added) # config.assets.precompile `= %w( admin.js admin.css ) # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies. # config.force_ssl = true ">Copy</button> </div></li> </ul> <h4 id="config-environments-test-rb"><a class="anchorlink" href="#config-environments-test-rb" data-turbo="false"><span>1.2.5</span> config/environments/test.rb</a></h4><div class="interstitial code"> <pre><code class="highlight ruby"><span class="c1"># Configure static asset server for tests with Cache-Control for performance</span> <span class="n">config</span><span class="p">.</span><span class="nf">serve_static_assets</span> <span class="o">=</span> <span class="kp">true</span> <span class="n">config</span><span class="p">.</span><span class="nf">static_cache_control</span> <span class="o">=</span> <span class="s2">"public, max-age=3600"</span> </code></pre> <button class="clipboard-button" data-clipboard-text="# Configure static asset server for tests with Cache-Control for performance config.serve_static_assets = true config.static_cache_control = &quot;public, max-age=3600&quot; ">Copy</button> </div> <h4 id="config-initializers-wrap-parameters-rb"><a class="anchorlink" href="#config-initializers-wrap-parameters-rb" data-turbo="false"><span>1.2.6</span> config/initializers/wrap_parameters.rb</a></h4> <ul> <li><p>Add this file with the following contents, if you wish to wrap parameters into a nested hash. This is on by default in new applications.</p><div class="interstitial code"> <pre><code class="highlight ruby"><span class="c1"># Be sure to restart your server when you modify this file.</span> <span class="c1"># This file contains settings for ActionController::ParamsWrapper which</span> <span class="c1"># is enabled by default.</span> <span class="c1"># Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array.</span> <span class="no">ActiveSupport</span><span class="p">.</span><span class="nf">on_load</span><span class="p">(</span><span class="ss">:action_controller</span><span class="p">)</span> <span class="k">do</span> <span class="n">wrap_parameters</span> <span class="ss">:format</span> <span class="o">=&gt;</span> <span class="p">[</span><span class="ss">:json</span><span class="p">]</span> <span class="k">end</span> <span class="c1"># Disable root element in JSON by default.</span> <span class="no">ActiveSupport</span><span class="p">.</span><span class="nf">on_load</span><span class="p">(</span><span class="ss">:active_record</span><span class="p">)</span> <span class="k">do</span> <span class="nb">self</span><span class="p">.</span><span class="nf">include_root_in_json</span> <span class="o">=</span> <span class="kp">false</span> <span class="k">end</span> </code></pre> <button class="clipboard-button" data-clipboard-text="# Be sure to restart your server when you modify this file. # This file contains settings for ActionController::ParamsWrapper which # is enabled by default. # Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array. ActiveSupport.on_load(:action_controller) do wrap_parameters :format => [:json] end # Disable root element in JSON by default. ActiveSupport.on_load(:active_record) do self.include_root_in_json = false end ">Copy</button> </div></li> </ul> <h4 id="remove-cache-and-concat-options-in-asset-helpers-references-in-views"><a class="anchorlink" href="#remove-cache-and-concat-options-in-asset-helpers-references-in-views" data-turbo="false"><span>1.2.7</span> Remove :cache and :concat options in asset helpers references in views</a></h4> <ul> <li>With the Asset Pipeline the :cache and :concat options aren't used anymore, delete these options from your views.</li> </ul> <h2 id="creating-a-rails-3-1-application"><a class="anchorlink" href="#creating-a-rails-3-1-application" data-turbo="false"><span>2</span> Creating a Rails 3.1 application</a></h2><div class="interstitial code"> <pre><code class="highlight console"><span class="gp">#</span><span class="w"> </span>You should have the <span class="s1">'rails'</span> RubyGem installed <span class="gp">$</span><span class="w"> </span><span class="nb">rails </span>new myapp <span class="gp">$</span><span class="w"> </span><span class="nb">cd </span>myapp </code></pre> <button class="clipboard-button" data-clipboard-text="rails new myapp cd myapp ">Copy</button> </div> <h3 id="vendoring-gems"><a class="anchorlink" href="#vendoring-gems" data-turbo="false"><span>2.1</span> Vendoring Gems</a></h3><p>Rails now uses a <code>Gemfile</code> in the application root to determine the gems you require for your application to start. This <code>Gemfile</code> is processed by the <a href="https://github.com/carlhuda/bundler">Bundler</a> gem, which then installs all your dependencies. It can even install all the dependencies locally to your application so that it doesn't depend on the system gems.</p><p>More information: - <a href="https://bundler.io/">bundler homepage</a></p><h3 id="living-on-the-edge"><a class="anchorlink" href="#living-on-the-edge" data-turbo="false"><span>2.2</span> Living on the Edge</a></h3><p><code>Bundler</code> and <code>Gemfile</code> makes freezing your Rails application easy as pie with the new dedicated <code>bundle</code> command. If you want to bundle straight from the Git repository, you can pass the <code>--edge</code> flag:</p><div class="interstitial code"> <pre><code class="highlight console"><span class="gp">$</span><span class="w"> </span><span class="nb">rails </span>new myapp <span class="nt">--edge</span> </code></pre> <button class="clipboard-button" data-clipboard-text="rails new myapp --edge ">Copy</button> </div> <p>If you have a local checkout of the Rails repository and want to generate an application using that, you can pass the <code>--dev</code> flag:</p><div class="interstitial code"> <pre><code class="highlight console"><span class="gp">$</span><span class="w"> </span><span class="nb">ruby</span> /path/to/rails/railties/bin/rails new myapp <span class="nt">--dev</span> </code></pre> <button class="clipboard-button" data-clipboard-text="ruby /path/to/rails/railties/bin/rails new myapp --dev ">Copy</button> </div> <h2 id="rails-architectural-changes"><a class="anchorlink" href="#rails-architectural-changes" data-turbo="false"><span>3</span> Rails Architectural Changes</a></h2><h3 id="assets-pipeline"><a class="anchorlink" href="#assets-pipeline" data-turbo="false"><span>3.1</span> Assets Pipeline</a></h3><p>The major change in Rails 3.1 is the Assets Pipeline. It makes CSS and JavaScript first-class code citizens and enables proper organization, including use in plugins and engines.</p><p>The assets pipeline is powered by <a href="https://github.com/rails/sprockets">Sprockets</a> and is covered in the <a href="asset_pipeline.html">Asset Pipeline</a> guide.</p><h3 id="http-streaming"><a class="anchorlink" href="#http-streaming" data-turbo="false"><span>3.2</span> HTTP Streaming</a></h3><p>HTTP Streaming is another change that is new in Rails 3.1. This lets the browser download your stylesheets and JavaScript files while the server is still generating the response. This requires Ruby 1.9.2, is opt-in and requires support from the web server as well, but the popular combo of NGINX and Unicorn is ready to take advantage of it.</p><h3 id="default-js-library-is-now-jquery"><a class="anchorlink" href="#default-js-library-is-now-jquery" data-turbo="false"><span>3.3</span> Default JS library is now jQuery</a></h3><p>jQuery is the default JavaScript library that ships with Rails 3.1. But if you use Prototype, it's simple to switch.</p><div class="interstitial code"> <pre><code class="highlight console"><span class="gp">$</span><span class="w"> </span><span class="nb">rails </span>new myapp <span class="nt">-j</span> prototype </code></pre> <button class="clipboard-button" data-clipboard-text="rails new myapp -j prototype ">Copy</button> </div> <h3 id="identity-map"><a class="anchorlink" href="#identity-map" data-turbo="false"><span>3.4</span> Identity Map</a></h3><p>Active Record has an Identity Map in Rails 3.1. An identity map keeps previously instantiated records and returns the object associated with the record if accessed again. The identity map is created on a per-request basis and is flushed at request completion.</p><p>Rails 3.1 comes with the identity map turned off by default.</p><h2 id="railties"><a class="anchorlink" href="#railties" data-turbo="false"><span>4</span> Railties</a></h2> <ul> <li><p>jQuery is the new default JavaScript library.</p></li> <li><p>jQuery and Prototype are no longer vendored and is provided from now on by the <code>jquery-rails</code> and <code>prototype-rails</code> gems.</p></li> <li><p>The application generator accepts an option <code>-j</code> which can be an arbitrary string. If passed "foo", the gem "foo-rails" is added to the <code>Gemfile</code>, and the application JavaScript manifest requires "foo" and "foo_ujs". Currently only "prototype-rails" and "jquery-rails" exist and provide those files via the asset pipeline.</p></li> <li><p>Generating an application or a plugin runs <code>bundle install</code> unless <code>--skip-gemfile</code> or <code>--skip-bundle</code> is specified.</p></li> <li><p>The controller and resource generators will now automatically produce asset stubs (this can be turned off with <code>--skip-assets</code>). These stubs will use CoffeeScript and Sass, if those libraries are available.</p></li> <li><p>Scaffold and app generators use the Ruby 1.9 style hash when running on Ruby 1.9. To generate old style hash, <code>--old-style-hash</code> can be passed.</p></li> <li><p>Scaffold controller generator creates format block for JSON instead of XML.</p></li> <li><p>Active Record logging is directed to STDOUT and shown inline in the console.</p></li> <li><p>Added <code>config.force_ssl</code> configuration which loads <code>Rack::SSL</code> middleware and force all requests to be under HTTPS protocol.</p></li> <li><p>Added <code>rails plugin new</code> command which generates a Rails plugin with gemspec, tests and a dummy application for testing.</p></li> <li><p>Added <code>Rack::Etag</code> and <code>Rack::ConditionalGet</code> to the default middleware stack.</p></li> <li><p>Added <code>Rack::Cache</code> to the default middleware stack.</p></li> <li><p>Engines received a major update - You can mount them at any path, enable assets, run generators, etc.</p></li> </ul> <h2 id="action-pack"><a class="anchorlink" href="#action-pack" data-turbo="false"><span>5</span> Action Pack</a></h2><h3 id="action-controller"><a class="anchorlink" href="#action-controller" data-turbo="false"><span>5.1</span> Action Controller</a></h3> <ul> <li><p>A warning is given out if the CSRF token authenticity cannot be verified.</p></li> <li><p>Specify <code>force_ssl</code> in a controller to force the browser to transfer data via HTTPS protocol on that particular controller. To limit to specific actions, <code>:only</code> or <code>:except</code> can be used.</p></li> <li><p>Sensitive query string parameters specified in <code>config.filter_parameters</code> will now be filtered out from the request paths in the log.</p></li> <li><p>URL parameters which return <code>nil</code> for <code>to_param</code> are now removed from the query string.</p></li> <li><p>Added <code>ActionController::ParamsWrapper</code> to wrap parameters into a nested hash, and will be turned on for JSON request in new applications by default. This can be customized in <code>config/initializers/wrap_parameters.rb</code>.</p></li> <li><p>Added <code>config.action_controller.include_all_helpers</code>. By default <code>helper :all</code> is done in <code>ActionController::Base</code>, which includes all the helpers by default. Setting <code>include_all_helpers</code> to <code>false</code> will result in including only application_helper and the helper corresponding to controller (like foo_helper for foo_controller).</p></li> <li><p><code>url_for</code> and named URL helpers now accept <code>:subdomain</code> and <code>:domain</code> as options.</p></li> <li><p>Added <code>Base.http_basic_authenticate_with</code> to do simple http basic authentication with a single class method call.</p><div class="interstitial code"> <pre><code class="highlight ruby"><span class="k">class</span> <span class="nc">PostsController</span> <span class="o">&lt;</span> <span class="no">ApplicationController</span> <span class="no">USER_NAME</span><span class="p">,</span> <span class="no">PASSWORD</span> <span class="o">=</span> <span class="s2">"dhh"</span><span class="p">,</span> <span class="s2">"secret"</span> <span class="n">before_filter</span> <span class="ss">:authenticate</span><span class="p">,</span> <span class="ss">:except</span> <span class="o">=&gt;</span> <span class="p">[</span> <span class="ss">:index</span> <span class="p">]</span> <span class="k">def</span> <span class="nf">index</span> <span class="n">render</span> <span class="ss">:text</span> <span class="o">=&gt;</span> <span class="s2">"Everyone can see me!"</span> <span class="k">end</span> <span class="k">def</span> <span class="nf">edit</span> <span class="n">render</span> <span class="ss">:text</span> <span class="o">=&gt;</span> <span class="s2">"I'm only accessible if you know the password"</span> <span class="k">end</span> <span class="kp">private</span> <span class="k">def</span> <span class="nf">authenticate</span> <span class="n">authenticate_or_request_with_http_basic</span> <span class="k">do</span> <span class="o">|</span><span class="n">user_name</span><span class="p">,</span> <span class="n">password</span><span class="o">|</span> <span class="n">user_name</span> <span class="o">==</span> <span class="no">USER_NAME</span> <span class="o">&amp;&amp;</span> <span class="n">password</span> <span class="o">==</span> <span class="no">PASSWORD</span> <span class="k">end</span> <span class="k">end</span> <span class="k">end</span> </code></pre> <button class="clipboard-button" data-clipboard-text="class PostsController < ApplicationController USER_NAME, PASSWORD = &quot;dhh&quot;, &quot;secret&quot; before_filter :authenticate, :except => [ :index ] def index render :text => &quot;Everyone can see me!&quot; end def edit render :text => &quot;I'm only accessible if you know the password&quot; end private def authenticate authenticate_or_request_with_http_basic do |user_name, password| user_name == USER_NAME &amp;&amp; password == PASSWORD end end end ">Copy</button> </div> <p>..can now be written as</p><div class="interstitial code"> <pre><code class="highlight ruby"><span class="k">class</span> <span class="nc">PostsController</span> <span class="o">&lt;</span> <span class="no">ApplicationController</span> <span class="n">http_basic_authenticate_with</span> <span class="ss">:name</span> <span class="o">=&gt;</span> <span class="s2">"dhh"</span><span class="p">,</span> <span class="ss">:password</span> <span class="o">=&gt;</span> <span class="s2">"secret"</span><span class="p">,</span> <span class="ss">:except</span> <span class="o">=&gt;</span> <span class="ss">:index</span> <span class="k">def</span> <span class="nf">index</span> <span class="n">render</span> <span class="ss">:text</span> <span class="o">=&gt;</span> <span class="s2">"Everyone can see me!"</span> <span class="k">end</span> <span class="k">def</span> <span class="nf">edit</span> <span class="n">render</span> <span class="ss">:text</span> <span class="o">=&gt;</span> <span class="s2">"I'm only accessible if you know the password"</span> <span class="k">end</span> <span class="k">end</span> </code></pre> <button class="clipboard-button" data-clipboard-text="class PostsController < ApplicationController http_basic_authenticate_with :name => &quot;dhh&quot;, :password => &quot;secret&quot;, :except => :index def index render :text => &quot;Everyone can see me!&quot; end def edit render :text => &quot;I'm only accessible if you know the password&quot; end end ">Copy</button> </div></li> <li><p>Added streaming support, you can enable it with:</p><div class="interstitial code"> <pre><code class="highlight ruby"><span class="k">class</span> <span class="nc">PostsController</span> <span class="o">&lt;</span> <span class="no">ActionController</span><span class="o">::</span><span class="no">Base</span> <span class="n">stream</span> <span class="k">end</span> </code></pre> <button class="clipboard-button" data-clipboard-text="class PostsController < ActionController::Base stream end ">Copy</button> </div> <p>You can restrict it to some actions by using <code>:only</code> or <code>:except</code>. Please read the docs at <a href="https://api.rubyonrails.org/v3.1.0/classes/ActionController/Streaming.html"><code>ActionController::Streaming</code></a> for more information.</p></li> <li><p>The redirect route method now also accepts a hash of options which will only change the parts of the URL in question, or an object which responds to call, allowing for redirects to be reused.</p></li> </ul> <h3 id="action-dispatch"><a class="anchorlink" href="#action-dispatch" data-turbo="false"><span>5.2</span> Action Dispatch</a></h3> <ul> <li><p><code>config.action_dispatch.x_sendfile_header</code> now defaults to <code>nil</code> and <code>config/environments/production.rb</code> doesn't set any particular value for it. This allows servers to set it through <code>X-Sendfile-Type</code>.</p></li> <li><p><code>ActionDispatch::MiddlewareStack</code> now uses composition over inheritance and is no longer an array.</p></li> <li><p>Added <code>ActionDispatch::Request.ignore_accept_header</code> to ignore accept headers.</p></li> <li><p>Added <code>Rack::Cache</code> to the default stack.</p></li> <li><p>Moved etag responsibility from <code>ActionDispatch::Response</code> to the middleware stack.</p></li> <li><p>Rely on <code>Rack::Session</code> stores API for more compatibility across the Ruby world. This is backwards incompatible since <code>Rack::Session</code> expects <code>#get_session</code> to accept four arguments and requires <code>#destroy_session</code> instead of simply <code>#destroy</code>.</p></li> <li><p>Template lookup now searches further up in the inheritance chain.</p></li> </ul> <h3 id="action-view"><a class="anchorlink" href="#action-view" data-turbo="false"><span>5.3</span> Action View</a></h3> <ul> <li><p>Added an <code>:authenticity_token</code> option to <code>form_tag</code> for custom handling or to omit the token by passing <code>:authenticity_token =&gt; false</code>.</p></li> <li><p>Created <code>ActionView::Renderer</code> and specified an API for <code>ActionView::Context</code>.</p></li> <li><p>In place <code>SafeBuffer</code> mutation is prohibited in Rails 3.1.</p></li> <li><p>Added HTML5 <code>button_tag</code> helper.</p></li> <li><p><code>file_field</code> automatically adds <code>:multipart =&gt; true</code> to the enclosing form.</p></li> <li><p>Added a convenience idiom to generate HTML5 data-* attributes in tag helpers from a <code>:data</code> hash of options:</p><div class="interstitial code"> <pre><code class="highlight ruby"><span class="n">tag</span><span class="p">(</span><span class="s2">"div"</span><span class="p">,</span> <span class="ss">:data</span> <span class="o">=&gt;</span> <span class="p">{</span><span class="ss">:name</span> <span class="o">=&gt;</span> <span class="s1">'Stephen'</span><span class="p">,</span> <span class="ss">:city_state</span> <span class="o">=&gt;</span> <span class="sx">%w(Chicago IL)</span><span class="p">})</span> <span class="c1"># =&gt; &lt;div data-name="Stephen" data-city-state="[&amp;quot;Chicago&amp;quot;,&amp;quot;IL&amp;quot;]" /&gt;</span> </code></pre> <button class="clipboard-button" data-clipboard-text="tag(&quot;div&quot;, :data => {:name => 'Stephen', :city_state => %w(Chicago IL)}) # => <div data-name=&quot;Stephen&quot; data-city-state=&quot;[&amp;quot;Chicago&amp;quot;,&amp;quot;IL&amp;quot;]&quot; /> ">Copy</button> </div></li> </ul> <p>Keys are dasherized. Values are JSON-encoded, except for strings and symbols.</p> <ul> <li><p><code>csrf_meta_tag</code> is renamed to <code>csrf_meta_tags</code> and aliases <code>csrf_meta_tag</code> for backwards compatibility.</p></li> <li><p>The old template handler API is deprecated and the new API simply requires a template handler to respond to call.</p></li> <li><p>rhtml and rxml are finally removed as template handlers.</p></li> <li><p><code>config.action_view.cache_template_loading</code> is brought back which allows to decide whether templates should be cached or not.</p></li> <li><p>The submit form helper does not generate an id "object_name_id" anymore.</p></li> <li><p>Allows <code>FormHelper#form_for</code> to specify the <code>:method</code> as a direct option instead of through the <code>:html</code> hash. <code>form_for(@post, remote: true, method: :delete)</code> instead of <code>form_for(@post, remote: true, html: { method: :delete })</code>.</p></li> <li><p>Provided <code>JavaScriptHelper#j()</code> as an alias for <code>JavaScriptHelper#escape_javascript()</code>. This supersedes the <code>Object#j()</code> method that the JSON gem adds within templates using the JavaScriptHelper.</p></li> <li><p>Allows AM/PM format in datetime selectors.</p></li> <li><p><code>auto_link</code> has been removed from Rails and extracted into the <a href="https://github.com/tenderlove/rails_autolink">rails_autolink gem</a></p></li> </ul> <h2 id="active-record"><a class="anchorlink" href="#active-record" data-turbo="false"><span>6</span> Active Record</a></h2> <ul> <li><p>Added a class method <code>pluralize_table_names</code> to singularize/pluralize table names of individual models. Previously this could only be set globally for all models through <code>ActiveRecord::Base.pluralize_table_names</code>.</p><div class="interstitial code"> <pre><code class="highlight ruby"><span class="k">class</span> <span class="nc">User</span> <span class="o">&lt;</span> <span class="no">ActiveRecord</span><span class="o">::</span><span class="no">Base</span> <span class="nb">self</span><span class="p">.</span><span class="nf">pluralize_table_names</span> <span class="o">=</span> <span class="kp">false</span> <span class="k">end</span> </code></pre> <button class="clipboard-button" data-clipboard-text="class User < ActiveRecord::Base self.pluralize_table_names = false end ">Copy</button> </div></li> <li><p>Added block setting of attributes to singular associations. The block will get called after the instance is initialized.</p><div class="interstitial code"> <pre><code class="highlight ruby"><span class="k">class</span> <span class="nc">User</span> <span class="o">&lt;</span> <span class="no">ActiveRecord</span><span class="o">::</span><span class="no">Base</span> <span class="n">has_one</span> <span class="ss">:account</span> <span class="k">end</span> <span class="n">user</span><span class="p">.</span><span class="nf">build_account</span><span class="p">{</span> <span class="o">|</span><span class="n">a</span><span class="o">|</span> <span class="n">a</span><span class="p">.</span><span class="nf">credit_limit</span> <span class="o">=</span> <span class="mf">100.0</span> <span class="p">}</span> </code></pre> <button class="clipboard-button" data-clipboard-text="class User < ActiveRecord::Base has_one :account end user.build_account{ |a| a.credit_limit = 100.0 } ">Copy</button> </div></li> <li><p>Added <code>ActiveRecord::Base.attribute_names</code> to return a list of attribute names. This will return an empty array if the model is abstract or the table does not exist.</p></li> <li><p>CSV Fixtures are deprecated and support will be removed in Rails 3.2.0.</p></li> <li><p><code>ActiveRecord#new</code>, <code>ActiveRecord#create</code> and <code>ActiveRecord#update_attributes</code> all accept a second hash as an option that allows you to specify which role to consider when assigning attributes. This is built on top of Active Model's new mass assignment capabilities:</p><div class="interstitial code"> <pre><code class="highlight ruby"><span class="k">class</span> <span class="nc">Post</span> <span class="o">&lt;</span> <span class="no">ActiveRecord</span><span class="o">::</span><span class="no">Base</span> <span class="n">attr_accessible</span> <span class="ss">:title</span> <span class="n">attr_accessible</span> <span class="ss">:title</span><span class="p">,</span> <span class="ss">:published_at</span><span class="p">,</span> <span class="ss">:as</span> <span class="o">=&gt;</span> <span class="ss">:admin</span> <span class="k">end</span> <span class="no">Post</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="n">params</span><span class="p">[</span><span class="ss">:post</span><span class="p">],</span> <span class="ss">:as</span> <span class="o">=&gt;</span> <span class="ss">:admin</span><span class="p">)</span> </code></pre> <button class="clipboard-button" data-clipboard-text="class Post < ActiveRecord::Base attr_accessible :title attr_accessible :title, :published_at, :as => :admin end Post.new(params[:post], :as => :admin) ">Copy</button> </div></li> <li><p><code>default_scope</code> can now take a block, lambda, or any other object which responds to call for lazy evaluation.</p></li> <li><p>Default scopes are now evaluated at the latest possible moment, to avoid problems where scopes would be created which would implicitly contain the default scope, which would then be impossible to get rid of via Model.unscoped.</p></li> <li><p>PostgreSQL adapter only supports PostgreSQL version 8.2 and higher.</p></li> <li><p><code>ConnectionManagement</code> middleware is changed to clean up the connection pool after the rack body has been flushed.</p></li> <li><p>Added an <code>update_column</code> method on Active Record. This new method updates a given attribute on an object, skipping validations and callbacks. It is recommended to use <code>update_attributes</code> or <code>update_attribute</code> unless you are sure you do not want to execute any callback, including the modification of the <code>updated_at</code> column. It should not be called on new records.</p></li> <li><p>Associations with a <code>:through</code> option can now use any association as the through or source association, including other associations which have a <code>:through</code> option and <code>has_and_belongs_to_many</code> associations.</p></li> <li><p>The configuration for the current database connection is now accessible via <code>ActiveRecord::Base.connection_config</code>.</p></li> <li><p>limits and offsets are removed from COUNT queries unless both are supplied.</p><div class="interstitial code"> <pre><code class="highlight ruby"><span class="no">People</span><span class="p">.</span><span class="nf">limit</span><span class="p">(</span><span class="mi">1</span><span class="p">).</span><span class="nf">count</span> <span class="c1"># =&gt; 'SELECT COUNT(*) FROM people'</span> <span class="no">People</span><span class="p">.</span><span class="nf">offset</span><span class="p">(</span><span class="mi">1</span><span class="p">).</span><span class="nf">count</span> <span class="c1"># =&gt; 'SELECT COUNT(*) FROM people'</span> <span class="no">People</span><span class="p">.</span><span class="nf">limit</span><span class="p">(</span><span class="mi">1</span><span class="p">).</span><span class="nf">offset</span><span class="p">(</span><span class="mi">1</span><span class="p">).</span><span class="nf">count</span> <span class="c1"># =&gt; 'SELECT COUNT(*) FROM people LIMIT 1 OFFSET 1'</span> </code></pre> <button class="clipboard-button" data-clipboard-text="People.limit(1).count # => 'SELECT COUNT(*) FROM people' People.offset(1).count # => 'SELECT COUNT(*) FROM people' People.limit(1).offset(1).count # => 'SELECT COUNT(*) FROM people LIMIT 1 OFFSET 1' ">Copy</button> </div></li> <li><p><code>ActiveRecord::Associations::AssociationProxy</code> has been split. There is now an <code>Association</code> class (and subclasses) which are responsible for operating on associations, and then a separate, thin wrapper called <code>CollectionProxy</code>, which proxies collection associations. This prevents namespace pollution, separates concerns, and will allow further refactorings.</p></li> <li><p>Singular associations (<code>has_one</code>, <code>belongs_to</code>) no longer have a proxy and simply returns the associated record or <code>nil</code>. This means that you should not use undocumented methods such as <code>bob.mother.create</code> - use <code>bob.create_mother</code> instead.</p></li> <li><p>Support the <code>:dependent</code> option on <code>has_many :through</code> associations. For historical and practical reasons, <code>:delete_all</code> is the default deletion strategy employed by <code>association.delete(*records)</code>, despite the fact that the default strategy is <code>:nullify</code> for regular has_many. Also, this only works at all if the source reflection is a belongs_to. For other situations, you should directly modify the through association.</p></li> <li><p>The behavior of <code>association.destroy</code> for <code>has_and_belongs_to_many</code> and <code>has_many :through</code> is changed. From now on, 'destroy' or 'delete' on an association will be taken to mean 'get rid of the link', not (necessarily) 'get rid of the associated records'.</p></li> <li><p>Previously, <code>has_and_belongs_to_many.destroy(*records)</code> would destroy the records themselves. It would not delete any records in the join table. Now, it deletes the records in the join table.</p></li> <li><p>Previously, <code>has_many_through.destroy(*records)</code> would destroy the records themselves, and the records in the join table. [Note: This has not always been the case; previous version of Rails only deleted the records themselves.] Now, it destroys only the records in the join table.</p></li> <li><p>Note that this change is backwards-incompatible to an extent, but there is unfortunately no way to 'deprecate' it before changing it. The change is being made in order to have consistency as to the meaning of 'destroy' or 'delete' across the different types of associations. If you wish to destroy the records themselves, you can do <code>records.association.each(&amp;:destroy)</code>.</p></li> <li><p>Add <code>:bulk =&gt; true</code> option to <code>change_table</code> to make all the schema changes defined in a block using a single ALTER statement.</p><div class="interstitial code"> <pre><code class="highlight ruby"><span class="n">change_table</span><span class="p">(</span><span class="ss">:users</span><span class="p">,</span> <span class="ss">:bulk</span> <span class="o">=&gt;</span> <span class="kp">true</span><span class="p">)</span> <span class="k">do</span> <span class="o">|</span><span class="n">t</span><span class="o">|</span> <span class="n">t</span><span class="p">.</span><span class="nf">string</span> <span class="ss">:company_name</span> <span class="n">t</span><span class="p">.</span><span class="nf">change</span> <span class="ss">:birthdate</span><span class="p">,</span> <span class="ss">:datetime</span> <span class="k">end</span> </code></pre> <button class="clipboard-button" data-clipboard-text="change_table(:users, :bulk => true) do |t| t.string :company_name t.change :birthdate, :datetime end ">Copy</button> </div></li> <li><p>Removed support for accessing attributes on a <code>has_and_belongs_to_many</code> join table. <code>has_many :through</code> needs to be used.</p></li> <li><p>Added a <code>create_association!</code> method for <code>has_one</code> and <code>belongs_to</code> associations.</p></li> <li><p>Migrations are now reversible, meaning that Rails will figure out how to reverse your migrations. To use reversible migrations, just define the <code>change</code> method.</p><div class="interstitial code"> <pre><code class="highlight ruby"><span class="k">class</span> <span class="nc">MyMigration</span> <span class="o">&lt;</span> <span class="no">ActiveRecord</span><span class="o">::</span><span class="no">Migration</span> <span class="k">def</span> <span class="nf">change</span> <span class="n">create_table</span><span class="p">(</span><span class="ss">:horses</span><span class="p">)</span> <span class="k">do</span> <span class="o">|</span><span class="n">t</span><span class="o">|</span> <span class="n">t</span><span class="p">.</span><span class="nf">column</span> <span class="ss">:content</span><span class="p">,</span> <span class="ss">:text</span> <span class="n">t</span><span class="p">.</span><span class="nf">column</span> <span class="ss">:remind_at</span><span class="p">,</span> <span class="ss">:datetime</span> <span class="k">end</span> <span class="k">end</span> <span class="k">end</span> </code></pre> <button class="clipboard-button" data-clipboard-text="class MyMigration < ActiveRecord::Migration def change create_table(:horses) do |t| t.column :content, :text t.column :remind_at, :datetime end end end ">Copy</button> </div></li> <li><p>Some things cannot be automatically reversed for you. If you know how to reverse those things, you should define <code>up</code> and <code>down</code> in your migration. If you define something in change that cannot be reversed, an <code>IrreversibleMigration</code> exception will be raised when going down.</p></li> <li><p>Migrations now use instance methods rather than class methods:</p><div class="interstitial code"> <pre><code class="highlight ruby"><span class="k">class</span> <span class="nc">FooMigration</span> <span class="o">&lt;</span> <span class="no">ActiveRecord</span><span class="o">::</span><span class="no">Migration</span> <span class="k">def</span> <span class="nf">up</span> <span class="c1"># Not self.up</span> <span class="c1"># ...</span> <span class="k">end</span> <span class="k">end</span> </code></pre> <button class="clipboard-button" data-clipboard-text="class FooMigration < ActiveRecord::Migration def up # Not self.up # ... end end ">Copy</button> </div></li> <li><p>Migration files generated from model and constructive migration generators (for example, add_name_to_users) use the reversible migration's <code>change</code> method instead of the ordinary <code>up</code> and <code>down</code> methods.</p></li> <li><p>Removed support for interpolating string SQL conditions on associations. Instead, a proc should be used.</p><div class="interstitial code"> <pre><code class="highlight ruby"><span class="n">has_many</span> <span class="ss">:things</span><span class="p">,</span> <span class="ss">:conditions</span> <span class="o">=&gt;</span> <span class="s1">'foo = #{bar}'</span> <span class="c1"># before</span> <span class="n">has_many</span> <span class="ss">:things</span><span class="p">,</span> <span class="ss">:conditions</span> <span class="o">=&gt;</span> <span class="nb">proc</span> <span class="p">{</span> <span class="s2">"foo = </span><span class="si">#{</span><span class="n">bar</span><span class="si">}</span><span class="s2">"</span> <span class="p">}</span> <span class="c1"># after</span> </code></pre> <button class="clipboard-button" data-clipboard-text="has_many :things, :conditions => 'foo = #{bar}' # before has_many :things, :conditions => proc { &quot;foo = #{bar}&quot; } # after ">Copy</button> </div> <p>Inside the proc, <code>self</code> is the object which is the owner of the association, unless you are eager loading the association, in which case <code>self</code> is the class which the association is within.</p><p>You can have any "normal" conditions inside the proc, so the following will work too:</p><div class="interstitial code"> <pre><code class="highlight ruby"><span class="n">has_many</span> <span class="ss">:things</span><span class="p">,</span> <span class="ss">:conditions</span> <span class="o">=&gt;</span> <span class="nb">proc</span> <span class="p">{</span> <span class="p">[</span><span class="s2">"foo = ?"</span><span class="p">,</span> <span class="n">bar</span><span class="p">]</span> <span class="p">}</span> </code></pre> <button class="clipboard-button" data-clipboard-text="has_many :things, :conditions => proc { [&quot;foo = ?&quot;, bar] } ">Copy</button> </div></li> <li><p>Previously <code>:insert_sql</code> and <code>:delete_sql</code> on <code>has_and_belongs_to_many</code> association allowed you to call 'record' to get the record being inserted or deleted. This is now passed as an argument to the proc.</p></li> <li><p>Added <code>ActiveRecord::Base#has_secure_password</code> (via <code>ActiveModel::SecurePassword</code>) to encapsulate dead-simple password usage with BCrypt encryption and salting.</p><div class="interstitial code"> <pre><code class="highlight ruby"><span class="c1"># Schema: User(name:string, password_digest:string, password_salt:string)</span> <span class="k">class</span> <span class="nc">User</span> <span class="o">&lt;</span> <span class="no">ActiveRecord</span><span class="o">::</span><span class="no">Base</span> <span class="n">has_secure_password</span> <span class="k">end</span> </code></pre> <button class="clipboard-button" data-clipboard-text="# Schema: User(name:string, password_digest:string, password_salt:string) class User < ActiveRecord::Base has_secure_password end ">Copy</button> </div></li> <li><p>When a model is generated <code>add_index</code> is added by default for <code>belongs_to</code> or <code>references</code> columns.</p></li> <li><p>Setting the id of a <code>belongs_to</code> object will update the reference to the object.</p></li> <li><p><code>ActiveRecord::Base#dup</code> and <code>ActiveRecord::Base#clone</code> semantics have changed to closer match normal Ruby dup and clone semantics.</p></li> <li><p>Calling <code>ActiveRecord::Base#clone</code> will result in a shallow copy of the record, including copying the frozen state. No callbacks will be called.</p></li> <li><p>Calling <code>ActiveRecord::Base#dup</code> will duplicate the record, including calling after initialize hooks. Frozen state will not be copied, and all associations will be cleared. A duped record will return <code>true</code> for <code>new_record?</code>, have a <code>nil</code> id field, and is saveable.</p></li> <li><p>The query cache now works with prepared statements. No changes in the applications are required.</p></li> </ul> <h2 id="active-model"><a class="anchorlink" href="#active-model" data-turbo="false"><span>7</span> Active Model</a></h2> <ul> <li><p><code>attr_accessible</code> accepts an option <code>:as</code> to specify a role.</p></li> <li><p><code>InclusionValidator</code>, <code>ExclusionValidator</code>, and <code>FormatValidator</code> now accepts an option which can be a proc, a lambda, or anything that respond to <code>call</code>. This option will be called with the current record as an argument and returns an object which respond to <code>include?</code> for <code>InclusionValidator</code> and <code>ExclusionValidator</code>, and returns a regular expression object for <code>FormatValidator</code>.</p></li> <li><p>Added <code>ActiveModel::SecurePassword</code> to encapsulate dead-simple password usage with BCrypt encryption and salting.</p></li> <li><p><code>ActiveModel::AttributeMethods</code> allows attributes to be defined on demand.</p></li> <li><p>Added support for selectively enabling and disabling observers.</p></li> <li><p>Alternate <code>I18n</code> namespace lookup is no longer supported.</p></li> </ul> <h2 id="active-resource"><a class="anchorlink" href="#active-resource" data-turbo="false"><span>8</span> Active Resource</a></h2> <ul> <li><p>The default format has been changed to JSON for all requests. If you want to continue to use XML you will need to set <code>self.format = :xml</code> in the class. For example,</p><div class="interstitial code"> <pre><code class="highlight ruby"><span class="k">class</span> <span class="nc">User</span> <span class="o">&lt;</span> <span class="no">ActiveResource</span><span class="o">::</span><span class="no">Base</span> <span class="nb">self</span><span class="p">.</span><span class="nf">format</span> <span class="o">=</span> <span class="ss">:xml</span> <span class="k">end</span> </code></pre> <button class="clipboard-button" data-clipboard-text="class User < ActiveResource::Base self.format = :xml end ">Copy</button> </div></li> </ul> <h2 id="active-support"><a class="anchorlink" href="#active-support" data-turbo="false"><span>9</span> Active Support</a></h2> <ul> <li><p><code>ActiveSupport::Dependencies</code> now raises <code>NameError</code> if it finds an existing constant in <code>load_missing_constant</code>.</p></li> <li><p>Added a new reporting method <code>Kernel#quietly</code> which silences both <code>STDOUT</code> and <code>STDERR</code>.</p></li> <li><p>Added <code>String#inquiry</code> as a convenience method for turning a String into a <code>StringInquirer</code> object.</p></li> <li><p>Added <code>Object#in?</code> to test if an object is included in another object.</p></li> <li><p><code>LocalCache</code> strategy is now a real middleware class and no longer an anonymous class.</p></li> <li><p><code>ActiveSupport::Dependencies::ClassCache</code> class has been introduced for holding references to reloadable classes.</p></li> <li><p><code>ActiveSupport::Dependencies::Reference</code> has been refactored to take direct advantage of the new <code>ClassCache</code>.</p></li> <li><p>Backports <code>Range#cover?</code> as an alias for <code>Range#include?</code> in Ruby 1.8.</p></li> <li><p>Added <code>weeks_ago</code> and <code>prev_week</code> to Date/DateTime/Time.</p></li> <li><p>Added <code>before_remove_const</code> callback to <code>ActiveSupport::Dependencies.remove_unloadable_constants!</code>.</p></li> </ul> <p>Deprecations:</p> <ul> <li><code>ActiveSupport::SecureRandom</code> is deprecated in favor of <code>SecureRandom</code> from the Ruby standard library.</li> </ul> <h2 id="credits"><a class="anchorlink" href="#credits" data-turbo="false"><span>10</span> Credits</a></h2><p>See the <a href="https://contributors.rubyonrails.org/">full list of contributors to Rails</a> for the many people who spent many hours making Rails, the stable and robust framework it is. Kudos to all of them.</p><p>Rails 3.1 Release Notes were compiled by <a href="https://github.com/vijaydev">Vijay Dev</a></p> </section> <hr> <footer aria-labelledby="heading-feedback" role="region"> <h2 id="heading-feedback">Feedback</h2> <p> You're encouraged to help improve the quality of this guide. </p> <p> Please contribute if you see any typos or factual errors. To get started, you can read our <a href="https://edgeguides.rubyonrails.org/contributing_to_ruby_on_rails.html#contributing-to-the-rails-documentation">documentation contributions</a> section. </p> <p> You may also find incomplete content or stuff that is not up to date. Please do add any missing documentation for main. Make sure to check <a href="https://edgeguides.rubyonrails.org">Edge Guides</a> first to verify if the issues are already fixed or not on the main branch. Check the <a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a> for style and conventions. </p> <p> If for whatever reason you spot something to fix but cannot patch it yourself, please <a href="https://github.com/rails/rails/issues">open an issue</a>. </p> <p>And last but not least, any kind of discussion regarding Ruby on Rails documentation is very welcome on the <a href="https://discuss.rubyonrails.org/c/rubyonrails-docs">official Ruby on Rails Forum</a>. </p> </footer> </div> </article> </main> <hr class="hide" /> <footer id="complementary"> <div class="wrapper"> <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> <p>"Rails", "Ruby on Rails", and the Rails logo are trademarks of David Heinemeier Hansson. All rights reserved.</p> </div> </footer> <a href="#main-skip-link" class="back-to-top" data-turbo="false"><span class="visibly-hidden">Back to top</span></a> </body> </html>

Pages: 1 2 3 4 5 6 7 8 9 10