10 cool things in Rails 2.3
Luke Francl
Boots Faster in Development n no ! Rails app (real time) c ly Empty l fi a nti t To cie time script/server -d s
Rails 2.2
Rails 2.3
1.461s
0.869s
Support for Rails Engines
Photo: Wikipedia
Routing Improvements
Routing Improvements RESTful routes use 50% less memory because formatted_* routes are no longer generated. Before: formatted_users_path('xml') formatted_users_path(:format => 'xml')
Now: users_path(:format => 'xml')
Routing Improvements Routes no longer inherit :only and :except. Before: map.resources :users, :only => :show do |user| user.resources :hobbies, :except => :none end
Now: map.resources :users, :only => :show do |user| user.resources :hobbies end
JSON Improvements
JSON Improvements JSON keys are always quoted, per the spec. Before: {123 => 'abc'}.to_json => '{123: "abc"}'
Now: {123 => 'abc'}.to_json => '{"123": "abc"}'
JSON Improvements Unicode escape sequences are unescaped. Before: ActiveSupport::JSON.decode ("{'hello': 'fa\\u00e7ade'}") => {"hello"=>"fa\\u00e7ade"}
Now: ActiveSupport::JSON.decode ("{'hello': 'fa\u00e7ade'}") => {"hello"=>"façade"}
Default Scopes default_scope :order => 'name asc' # will use default scope User.all # will use passed in order option. User.all(:order => 'name desc') puts User.all.map { |u| "#{u.id} - #{u.name}" } 3 - Alice 2 - Bob 1 - George
Nested Transactions User.transaction do user = User.create(:name => 'Luke') User.transaction(:requires_new => true) do user.hobbies.create(:name => 'Knitting') raise ActiveRecord::Rollback("Oh noes!!!") end end
Asset Host Objects class SslAssetHost def call(source, request) if request.ssl? "#{request.protocol}#{request.host_with_port}" else "#{request.protocol}assets.example.com" end end end
Override Timestamp Fields User.create(:name => "Alice", :created_at => 3.weeks.ago, :updated_at => 2.weeks.ago) => #<User id: 3, name: "Alice", created_at: "2009-03-08 00:06:58", updated_at: "2009-03-15 00:06:58">
Nested Attributes... class User < ActiveRecord::Base has_many :hobbies, :dependent => :destroy accepts_nested_attributes_for :hobbies end user = User.create(:name => 'Stan', :hobbies_attributes => [{:name => 'Waterskiing'}, {:name => 'Hiking'}])
...and Forms <% form_for(@user) do |f| %> <%= f.label :name, "User name:" %> <%= f.text_field :name %>
Hobbies
<% f.fields_for(:hobbies) do |hf| %> <%= hf.label :name, "Hobby name:" %> <%= hf.text_field :name %> <% end %> <%= f.submit 'Create' %> <% end %>
Rails Metal \m/ class UsersApi def self.call(env) if env['PATH_INFO'] =~ /^\/users.js/ [200, {'Content-Type' => 'application/json'}, User.all.to_json] else # Return 404 to let Rails handle the request [404, {'Content-Type' => 'text/html'}, 'not found'] end end end
Thanks! Blog post: http://railspikes.com/2009/3/30/10-cool-things-in-rails-23
Slides: http://slideshare.net/lukefrancl/10-cool-things-about-rails-23
Code: http://bitbucket.org/look/rails23-10coolfeatures/
Rails 2.3 Release Notes: http://guides.rubyonrails.org/2_3_release_notes.html