5

I am a beginner in Rails. I use 2.3.X.

I just saw Rails 3 is pre-released [edit: now in release candidate!]. I will most probably eventually switch to it.

What are the common coding habits in 2.3 I should not take, so that the switch is as smooth as possible ?

Edit:

I've done my homework and read the Release notes. But they are far from clear for the most crucial points, for example :

1.5 New APIs

Both the router and query interface have seen significant, breaking changes. There is a backwards compatibility layer that is in place and will be supported until the 3.1 release.

This is not comprehensive enough for a beginner like me. What will break ? What could I do already in 2.3.X to avoid having troubles later ?

glmxndr
  • 45,516
  • 29
  • 93
  • 118
  • Did you have a look at Jeremy McAnally's posts as I suggested in my answer? I don't think you can get more comprehensive guides to upgrading to Rails 3 these days. Not only he guides you through the whole process, he even wrote a gem to make the transition as painless as possible. – Milan Novota Feb 03 '10 at 09:51
  • I understand, Milan, but the topic is not how to migrate, but how to code so that I don't run into too many problems during the migration. I understand the value of your answer and of the link you gave, though. I learned, for example, that I must keep the routing simple. That's already a valuable information. If that's all I need to know, fine. But maybe there are other points. – glmxndr Feb 03 '10 at 10:38

3 Answers3

12

Looking at my personal coding habits (I have been using Rails since 1.2.x), here's a list of API changes you can anticipate according to Rails 3 release notes.

find(:all)

Avoid the usage of:

Model.find(:all)
Model.find(:first)
Model.find(:last)

in favour of:

Model.all
Model.first
Model.last

Complex queries

Avoid the composition of complex queries in favor of named scopes.

Anticipate Arel

Rails 3 offers a much cleaner approach for dealing with ActiveRecord conditions and options. You can anticipate it creating custom named scopes.

class Model
  named_scope :limit, lambda { |value| { :limit => value }}
end

# old way
records = Model.all(:limit => 3)

# new way
records = Model.limit(3).all

# you can also take advantage of lazy evaluation
records = Model.limit(3)
# then in your view
records.each { ... }

When upgrading to Rails 3, simply drop the named scope definition.

Constants

Avoid the usage of the following constants in favour of the corresponding Rails.x methods, already available in Rails 2.x.

  • RAILS_ROOT in favour of Rails.root,
  • RAILS_ENV in favour of Rails.env, and
  • RAILS_DEFAULT_LOGGER in favour of Rails.logger.

Unobtrusive Javascript

Avoid heavy JavaScript helpers in favour of unobtrusive JavaScript.

Gem dependencies

Keep your environment.rb as clean as possible in order to make easier the migration to Bundler. You can also anticipate the migration using Bundler today without Rails 3.

Michael Gaskill
  • 7,913
  • 10
  • 38
  • 43
Simone Carletti
  • 173,507
  • 49
  • 363
  • 364
3

The release notes are the most important thing to keep an eye on. Other than that Jeremy McAnally has some neat blog posts about the whole Rails 3 thing (and has just released a gem to help you with the migration).

Milan Novota
  • 15,506
  • 7
  • 54
  • 62
0

I'd say, read the rails release notes and see for yourself what seems the more surprising to you. A lot of stuff changed so reading this is definitively very important.

marcgg
  • 65,020
  • 52
  • 178
  • 231