Skip to content
This repository
Browse code

3.1 release notes Active Record changes, Architectural changes and

organizing sections.
  • Loading branch information...
commit ed246f53d245bb7131738303e42ad8745022f4e3 1 parent e3e1109
Vijay Dev vijaydev authored fxn committed

Showing 1 changed file with 165 additions and 24 deletions. Show diff stats Hide diff stats

  1. +165 24 railties/guides/source/3_1_release_notes.textile
189 railties/guides/source/3_1_release_notes.textile
Source Rendered
@@ -55,15 +55,23 @@ h3. Rails Architectural Changes
55 55
56 56 h4. Assets Pipeline
57 57
58   -TODO. point to assets guide, talk about rake assets:* tasks
  58 +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.
59 59
60   -h3. Documentation
  60 +The assets pipeline is powered by "Sprockets":https://github.com/sstephenson/sprockets and is covered in the "Asset Pipeline":asset_pipeline.html guide.
61 61
62   -The documentation in the Rails tree is being updated with all the API changes, additionally, the "Rails Edge Guides":http://edgeguides.rubyonrails.org/ are being updated one by one to reflect the changes in Rails 3.0. The guides at "guides.rubyonrails.org":http://guides.rubyonrails.org/ however will continue to contain only the stable version of Rails (at this point, version 2.3.5, until 3.0 is released).
  62 +h4. HTTP Streaming
63 63
64   -More Information: - "Rails Documentation Projects":http://weblog.rubyonrails.org/2009/1/15/rails-documentation-projects.
  64 +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.
65 65
66   -h3. Internationalization
  66 +h4. Default JS library is now jQuery
  67 +
  68 +jQuery is the default JavaScript library that ships with Rails 3.1. But if you use Prototype, it's simple to switch.
  69 +
  70 +h4. Identity Map
  71 +
  72 +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.
  73 +
  74 +Rails 3.1 comes with the identity map turned off by default.
67 75
68 76 h3. Railties
69 77
@@ -95,13 +103,9 @@ h3. Railties
95 103
96 104 h3. Action Pack
97 105
98   -TODO split items into controller/view sections.
99   -
100   -* A warning is given out if the CSRF token authenticity cannot be verified.
101   -
102   -* Allows AM/PM format in datetime selectors.
  106 +h4. Abstract Controller
103 107
104   -* auto_link has been removed from Rails and extracted into the "rails_autolink gem":https://github.com/tenderlove/rails_autolink
  108 +h4. Action Controller
105 109
106 110 * Added streaming support, you can enable it with:
107 111
@@ -111,13 +115,25 @@ class PostsController < ActionController::Base
111 115 end
112 116 </ruby>
113 117
114   -Please read the docs at <tt>ActionController::Streaming</tt> for more information. TODO add links to api docs.
  118 +Please read the docs at "<tt>ActionController::Streaming</tt>":http://edgeapi.rubyonrails.org/classes/ActionController/Streaming.html for more information.
  119 +
  120 +* Added <tt>ActionController::ParamsWrapper</tt> to wrap parameters into a nested hash, and will be turned on for JSON request in new applications by default. This can be customized by setting <tt>ActionController::Base.wrap_parameters</tt> in <tt>config/initializer/wrap_parameters.rb</tt>.
  121 +
  122 +h4. Action Dispatch
115 123
116 124 * Added <tt>ActionDispatch::Request.ignore_accept_header</tt> to ignore accept headers.
117 125
  126 +h4. Action View
  127 +
118 128 * Created <tt>ActionView::Renderer</tt> and specified an API for <tt>ActionView::Context</tt>.
119 129
120   -* Added <tt>ActionController::ParamsWrapper</tt> to wrap parameters into a nested hash, and will be turned on for JSON request in new applications by default. This can be customized by setting <tt>ActionController::Base.wrap_parameters</tt> in <tt>config/initializer/wrap_parameters.rb</tt>.
  130 +TODO
  131 +
  132 +* A warning is given out if the CSRF token authenticity cannot be verified.
  133 +
  134 +* Allows AM/PM format in datetime selectors.
  135 +
  136 +* auto_link has been removed from Rails and extracted into the "rails_autolink gem":https://github.com/tenderlove/rails_autolink
121 137
122 138 * Added Base.http_basic_authenticate_with to do simple http basic authentication with a single class method call.
123 139
@@ -209,19 +225,148 @@ Keys are dasherized. Values are JSON-encoded, except for strings and symbols.
209 225
210 226 * Added <tt>Rack::Cache</tt> to the default stack.
211 227
212   -h4. Abstract Controller
  228 +h3. Active Record
213 229
214   -h4. Action Controller
  230 +* Added a class method <tt>pluralize_table_names</tt> to singularize/pluralize table names of individual models. Previously this could only be set globally for all models through <tt>ActiveRecord::Base.pluralize_table_names</tt>.
  231 +<ruby>
  232 +class User < ActiveRecord::Base
  233 + self.pluralize_table_names = false
  234 +end
  235 +</ruby>
215 236
216   -h4. Action Dispatch
  237 +* Added block setting of attributes to singular associations. The block will get called after the instance is initialized.
217 238
218   -h4. Action View
  239 +<ruby>
  240 +class User < ActiveRecord::Base
  241 + has_one :account
  242 +end
219 243
220   -h3. Active Record
  244 +user.build_account{ |a| a.credit_limit => 100.0 }
  245 +</ruby>
221 246
222   -h3. Active Model
  247 +* Added <tt>ActiveRecord::Base.attribute_names</tt> to return a list of attribute names. This will return an empty array if the model is abstract or the table does not exist.
  248 +
  249 +* CSV Fixtures are deprecated and support will be removed in Rails 3.2.0
  250 +
  251 +* ActiveRecord#new, ActiveRecord#create and ActiveRecord#update_attributes 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 ActiveModel's new mass assignment capabilities:
  252 +
  253 +<ruby>
  254 +class Post < ActiveRecord::Base
  255 + attr_accessible :title
  256 + attr_accessible :title, :published_at, :as => :admin
  257 +end
  258 +
  259 +Post.new(params[:post], :as => :admin)
  260 +</ruby>
  261 +
  262 +* default_scope can now take a block, lambda, or any other object which responds to call for lazy evaluation:
  263 +
  264 +* 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.
  265 +
  266 +* PostgreSQL adapter only supports PostgreSQL version 8.2 and higher.
  267 +
  268 +* ConnectionManagement middleware is changed to clean up the connection pool after the rack body has been flushed.
  269 +
  270 +* Added an update_column method on ActiveRecord. This new method updates a given attribute on an object, skipping validations and callbacks. It is recommended to use #update_attribute unless you are sure you do not want to execute any callback, including the modification of the updated_at column. It should not be called on new records.
  271 +
  272 +* Associations with a :through option can now use any association as the through or source association, including other associations which have a :through option and has_and_belongs_to_many associations.
  273 +
  274 +* The configuration for the current database connection is now accessible via ActiveRecord::Base.connection_config.
223 275
224   -The major changes in Active Model are:
  276 +* limits and offsets are removed from COUNT queries unless both are supplied.
  277 +<ruby>
  278 +People.limit(1).count # => 'SELECT COUNT(*) FROM people'
  279 +People.offset(1).count # => 'SELECT COUNT(*) FROM people'
  280 +People.limit(1).offset(1).count # => 'SELECT COUNT(*) FROM people LIMIT 1 OFFSET 1'
  281 +</ruby>
  282 +
  283 +* <tt>ActiveRecord::Associations::AssociationProxy</tt> has been split. There is now an +Association+ class (and subclasses) which are responsible for operating on associations, and then a separate, thin wrapper +called+ CollectionProxy, which proxies collection associations. This prevents namespace pollution, separates concerns, and will allow further refactorings.
  284 +
  285 +* Singular associations (has_one, belongs_to) no longer have a proxy and simply returns the associated record or nil. This means that you should not use undocumented methods such as bob.mother.create - use bob.create_mother instead.
  286 +
  287 +* Support the :dependent option on has_many :through associations. For historical and practical reasons, :delete_all is the default deletion strategy employed by association.delete(*records), despite the fact that the default strategy is :nullify 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.
  288 +
  289 +* The behavior of association.destroy for has_and_belongs_to_many and has_many :through 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'.
  290 +
  291 +* Previously, has_and_belongs_to_many.destroy(*records) would destroy the records themselves. It would not delete any records in the join table. Now, it deletes the records in the join table.
  292 +
  293 +* Previously, has_many_through.destroy(*records) 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.
  294 +
  295 +* 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 records.association.each(&:destroy)
  296 +
  297 +* Add <tt>:bulk => true</tt> option to +change_table+ to make all the schema changes defined in a block using a single ALTER statement.
  298 +
  299 +<ruby>
  300 +change_table(:users, :bulk => true) do |t|
  301 + t.string :company_name
  302 + t.change :birthdate, :datetime
  303 +end
  304 +</ruby>
  305 +
  306 +* Removed support for accessing attributes on a +has_and_belongs_to_many+ join table. <tt>has_many :through</tt> needs to be used.
  307 +
  308 +* Added a +create_association!+ method for +has_one+ and +belongs_to+ associations.
  309 +
  310 +* Migrations are now reversible, meaning that Rails will figure out how to reverse your migrations. To use reversible migrations, just define the +change+ method.
  311 +<ruby>
  312 +class MyMigration < ActiveRecord::Migration
  313 + def change
  314 + create_table(:horses) do
  315 + t.column :content, :text
  316 + t.column :remind_at, :datetime
  317 + end
  318 + end
  319 +end
  320 +</ruby>
  321 +
  322 +* Some things cannot be automatically reversed for you. If you know how to reverse those things, you should define 'up' and 'down' in your migration. If you define something in change that cannot be reversed, an +IrreversibleMigration+ exception will be raised when going down.
  323 +
  324 +* Migrations now use instance methods rather than class methods:
  325 +<ruby>
  326 +class FooMigration < ActiveRecord::Migration
  327 + def up # Not self.up
  328 + ...
  329 + end
  330 +end
  331 +</ruby>
  332 +
  333 +* Migration files generated from model and constructive migration generators (for example, add_name_to_users) use the reversible migration's change method instead of the ordinary up and down methods.
  334 +
  335 +* Removed support for interpolating string SQL conditions on associations. Instead, a proc should be used.
  336 +
  337 +<ruby>
  338 +has_many :things, :conditions => 'foo = #{bar}' # before
  339 +has_many :things, :conditions => proc { "foo = #{bar}" } # after
  340 +</ruby>
  341 +
  342 +Inside the proc, 'self' is the object which is the owner of the association, unless you are eager loading the association, in which case 'self' is the class which the association is within.
  343 +
  344 +You can have any "normal" conditions inside the proc, so the following will work too:
  345 +<ruby>
  346 +has_many :things, :conditions => proc { ["foo = ?", bar] }
  347 +</ruby>
  348 +
  349 +* Previously :insert_sql and :delete_sql on has_and_belongs_to_many association allowed you to call 'record' to get the record being inserted or deleted. This is now passed as an argument to the proc.
  350 +
  351 +* Added <tt>ActiveRecord::Base#has_secure_password</tt> (via <tt>ActiveModel::SecurePassword</tt>) to encapsulate dead-simple password usage with BCrypt encryption and salting.
  352 +<ruby>
  353 +# Schema: User(name:string, password_digest:string, password_salt:string)
  354 +class User < ActiveRecord::Base
  355 + has_secure_password
  356 +end
  357 +</ruby>
  358 +
  359 +* When a model is generated +add_index+ is added by default for +belongs_to+ or +references+ columns.
  360 +
  361 +* Setting the id of a belongs_to object will update the reference to the object.
  362 +
  363 +* ActiveRecord::Base#dup and ActiveRecord::Base#clone semantics have changed to closer match normal Ruby dup and clone semantics.
  364 +
  365 +* Calling ActiveRecord::Base#clone will result in a shallow copy of the record, including copying the frozen state. No callbacks will be called.
  366 +
  367 +* Calling ActiveRecord::Base#dup 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 true for new_record?, have a nil id field, and is saveable.
  368 +
  369 +h3. Active Model
225 370
226 371 * +attr_accessible+ accepts an option +:as+ to specify a role.
227 372
@@ -233,8 +378,6 @@ The major changes in Active Model are:
233 378
234 379 h3. Active Resource
235 380
236   -The changes in Active Resource are:
237   -
238 381 * The default format has been changed to JSON for all requests. If you want to continue to use XML you will need to set <tt>self.format = :xml</tt> in the class. For example,
239 382
240 383 <ruby>
@@ -245,8 +388,6 @@ end
245 388
246 389 h3. Active Support
247 390
248   -The main changes in Active Support are:
249   -
250 391 * <tt>ActiveSupport::Dependencies</tt> now raises +NameError+ if it finds an existing constant in load_missing_constant.
251 392
252 393 * Added a new reporting method <tt>Kernel#quietly</tt> which silences both STDOUT and STDERR.

0 comments on commit ed246f5

Please sign in to comment.
Something went wrong with that request. Please try again.