Jim Lindley Notes

Rails 2.1

March 31st, 2008

Rails 2.1 is almost here, and there are two great resources for learning about it:

Ryan Daigle’s ‘What’s New In Edge Rails’, and Rails contrubutor Chu Yeow’s series: ‘Living on the Edge of Rails’.

Lots of fun stuff for this point release!

Rails 2.0 RC 2 Released

November 29th, 2007

It looks like we are a week away from the actual 2.0 release, you can check out the preview release by pulling from the gem from the Rails dev server, or freezing to edge in your project like so:

rake rails:freeze:edge TAG=rel_2-0-0_RC2

Remember, upgrade to the latest version of 1.2.x (1.2.6 at the moment), and check for any deprecation warnings before trying to run 2.0, unless you’ve been running on edge lately.

This release candidate contains many bug fixes but minimal new features over the last release candidate.

DHH has updated the docs for schema.rb, to reflect the fact that schema.rb is an important file that should be kept into version control. He recommends that production apps be initialized to their schema via this file, and not from migrations.

The rake db:reset task has been changed to reflect this philosophy, it will now run the db:schema:load instead of db:migrate after first dropping then recreating your database.

Migrations, as they build up over time, become increasingly fragile, whereas schema.rb is likely consistent to the current version of your schema and many fewer steps to load. One thing to keep in mind is that schema.rb does not preserve all the same setting that migrations do - character sets, collations, and other database dependent features. If you need those, switch from having a ruby schema.rb to an SQL schema file.

Switch to SQL by uncommenting this line in environment.rb:

# config.active_record.schema_format = :sql

Site News

November 10th, 2007

If you subscribe to this feed purely for the list of major changes to edge Rails, you might want to switch to this feed for that category of posts. I’m planning on talking about a wider range of subjects (still all programming and mostly Ruby related, and I won’t neglect Edge Rails) on this blog. I’ve also redirected the site from edgenotes.jimlindley.com to notes.jimlindley.com to reflect this change in direction, but it should be picked up up invisibly by feed readers.

Please leave comment if you find a problem with the site move.

The javascript libraries included with Rails have been bumped up to the latest release versions of each: Prototype 1.6 and Script.acul.ous 1.8. It looks like these are the versions that will make it into the final Rails 2.0 release.

Changeset #8087

rake db:rollback

October 27th, 2007

A new rake task, rollback, which will revert the latest applied migration.

rake db:rollback

Or for multiple steps back:

rake db:rollback STEP=2

Changeset #8039

Foxy Fixtures

October 27th, 2007

Just added to edge Rails is an update that makes fixtures worth using again. Instead of manually specifying the keys of associated fixtures or created/modified times, like this:

# in users.yaml

  jdoe:
    id: 1
    name: John Doe
    created_at: <%= Time.now %>
    modified_at: <%= Time.now %>

# in photos.yaml

  sunset_photo:
    id: 42
    title: Photo of a Sunset
    user_id: 1
    created_at: <%= Time.now %>
    modified_at: <%= Time.now %>

You can just give the name of the associated fixture and Rails will figure out keys for you:

# in users.yaml

  jdoe:
    name: John Doe

# in photos.yaml

  sunset_photo:
    title: Photo of a Sunset
    user: jdoe # user not user_id

For has_and_belongs_to_many assocations, you can specify the relations with a comma separted list of references to the associated fixtures:

# in tags.yaml

  red:
    name: Red
    items: fire_hydrant, rover

Changeset #8036, RDoc

Filter Chain Halting

October 21st, 2007

Controller filters (in 1.2.x and previous) stop when a filter returns false. Checked into edge today, and sure to show up in the next 2.0 RC, is a break to that behaviour.

In 2.0 filters can only halt the chain with a render or redirect (similar to the actual controller methods themselves). It was too easy for an errant filter halt to lead to a white screen of death. Be sure to comb through your code and consider how your execution flow will be affected.

Changeset #7984

Sexy Migration Addition

October 19th, 2007

A new foreign key helper for sexy migrations, ‘references’.

create_table :users do |t|
  # old: t.integer :profile_id
  t.references :profile 
end

It can also do polymorphic relations, like so (from the Rails docs)

create_table :taggings do |t| 
   t.references :tag 
   t.references :tagger, :polymorphic => true 
   t.references :taggable, 
                :polymorphic => { :default => 'Photo' } 
end

Changeset #7973

Error Message Options

October 15th, 2007

New in trunk, additional options for presenting model errors. You can now add :message or :header_message to ‘error_messages_for’, to override the default messages.

Currently the messages are based on the model name and error count, but those can be misleading in some presentations.

Changeset #7870