Rails 4.0 to 4.1 Upgrade

When updating from Rails 4.0 to 4.1, I ran into two issues:

The first was that has_and_belongs_to_many associations now seem to require a primary key on the join table. Easy enough to solve – just add a primary key to your join table.

# As a migration:

class AddPrimaryKeyToMissionsVendors < ActiveRecord::Migration
  def change
    add_column :missions_vendors, :id, :primary_key
  end
end

The second was that ActiveRecord #count queries have changed behaviour a bit. Before, the select clause defaulted to SELECT(*). So, even if you had something like:

User.select(:id, :username, :favourite_color).count

The query would end up in SQL as:

SELECT count(*) from users;

The newer version generates the (more accurate) SQL:

SELECT count(users.id, users.username, users.favourite_color) from users;

In my case this actually broke some places where I was implicitly relying on count(*). To fix this, I just changed count to count(:all).

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s