← Back to Upcase

Course changes for Rails 4 + Ruby 2 (+ gems)

(Paul Morganthall) #1

I’ve ignored the course recommendation to use Rails 1.9.3 and Rails 3.2.12. I also tend to use the latest versions of other gems, though I’m pretty quick to dial back a gem if I find an incompatibility. I do maintain a “course standard” environment to test against so I don’t misuse thoughtbot resources with these practices.

I’ll document changes I run into. Please add any you find too. Make sure to include relevant version numbers and links to docs if you can. If appropriate, note which class video and approximate time.

(Paul Morganthall) #2

Part 1 time 8:30: the root route

As of Rails v4.0.0.beta1, you no longer need to add via: :get to restrict the verbs matched, as get is now the default.

As of Rails v4.0.0.beta1, the root route no longer needs the to: option. The new helper looks like this:

root 'homes#show'

(Paul Morganthall) #3

Part 1 time 25:45: the belongs_to index

As of Rails 4, the generated migration for a belongs_to now includes the index on the column declaration line.

rails generate model shout body user:belongs_to

class CreateShouts < ActiveRecord::Migration
  def change
    create_table :shouts do |t|
      t.string :body
      t.belongs_to :user, index: true


The Rails 3 way adds a separate statement after the create_table:

add_index :shouts, :user_id

(Paul Morganthall) #4

Part 3 time 31:15: support for to_partial_path

extend ActiveModel::Naming

The statement above is added to handle Timeline's need for the to_partial_path method.

The to_partial_path method in Rails 4 is now located in ActiveModel::Conversion. I followed the example in the doc and used include rather than extend.

include ActiveModel::Conversion


(Paul Morganthall) #5

I checked earlier docs, and it looks like to_partial_path has been in ActiveModel::Conversion since Rails 3.2.1. So this is not really a Rails 4 change. Perhaps it is an error in the video? I’m not sure how @halogenandtoast got to_partial_path to work via ActiveModel::Naming.


(Paul Morganthall) #6

Part 1 time 38:45: order method accepts a hash

The shouts are ordered with default_scope:

default_scope { order('created_at DESC') }

As Matt mentions, that syntax (with the block) is Rails 4 compatible. If you omit the block, you’ll get a deprecation warning in Rails 4.

Rails 4 also allows passing a hash to order:

default_scope { order(created_at: :desc) }

It’s worth noting that while a block is enough for default_scope, the Rails 4 scope method requires a callable object. You may also use the a callable object for default_scope.

scope :active, -> { where(status: 'active') }


(Ben Orenstein) #7

This is super helpful @slothbear. Thanks for doing it!

(Jaroslav Janukevic) #8

In Rails 4. You don’t need module Concerns

This is enough /app/models/concerns/following.rb

module Following
  extend ActiveSupport::Concern

and in /app/models/user.rb

include Following

(Jaroslav Janukevic) #9

In part 4 towards the end, we use Solr. There is no rake task to run

rake sunspot:solr:start


wget -O lib/tasks/solr.rake https://gist.github.com/SoftMonkeyJapan/6068755/raw/127f39bfc159610098a9e6854764602189e5e6f9/solr.rake 

(Scott Hollinshead) #10

In Rails 4 the find_by_“column name” can now be written using the find_by method and passing a Hash.

Rails 3 Example:

user = User.find_by_email(params[:email])

Rails 4 Example:

user = User.find_by(email: params[:email])

(Ben Orenstein) #11

Really appreciate you guys posting this errata here.

By the way, we’ll start recording new versions of these this Friday. Updated versions are on the way!

(Sage Elliott) #12

Sorry to bump up such an old thread, but I was curious what happened to making updated versions or if you see Upcase updating some of the course material in the near future? Even though some some of the videos are a little outdated they’ve definitely been some of the best tutorials I’ve watched and would love to see everyone benefit from your excellent teaching style.

Edit: ^I feel like I wrote that prematurely, as the videos really do not have much you need to change for rails 4. I also kind of enjoyed having to poke around deeper to figure out the changes.

(Ben Orenstein) #13

It’s on our list, but at a somewhat lower priority than other things.

As you said, almost all the information is still valid and good, so the (quite large) effort of updating for a new version needs to be weighed carefully.