Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 : Anticipating migration for 2.3 beginners

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 ?

like image 364
glmxndr Avatar asked Feb 03 '10 09:02

glmxndr


People also ask

How do I run a specific migration in rails?

To run a specific migration up or down, use db:migrate:up or db:migrate:down . The version number in the above commands is the numeric prefix in the migration's filename. For example, to migrate to the migration 20160515085959_add_name_to_users. rb , you would use 20160515085959 as the version number.

What is ActiveRecord in Ruby on Rails?

Active Record is the M in MVC - the model - which is the layer of the system responsible for representing business data and logic. Active Record facilitates the creation and use of business objects whose data requires persistent storage to a database.

How do I view pending migrations in rails?

If you need a bash one-liner to determine whether to run a migration or not (e.g., only migrate in a Heroku release phase command when there is a pending migration), this works: (rails db:migrate:status | grep "^\s*down") && rails db:migrate || echo "No pending migrations found."


1 Answers

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.

like image 102
Simone Carletti Avatar answered Nov 11 '22 05:11

Simone Carletti