Mar
22

Ruby on Rails Quick Tip #1: Loading models in Rails 2.0

By Marco  //  Ruby on Rails  //  No Comments

Here is a quick tip, that i have learned, this applied to the book Beginning Ruby on Rails chapter 7 (page 206), in the book we were asked to load models (please remember that the book was published when the rails being used was 1.2.X, around 2007.

# Ruby on Rails - Quick Tip - Loading models in Ruby 2.X.X
# Book: Beginning Ruby on Rails Chapter 7 (page 206)
# Marco's notes: 2009.03.23

# We were asked to load models in the application controller 

  class ApplicationController < ActionController::Base
    model :cart
    model :purchases
  end

# Results in this:
# undefined method `model' for ApplicationController:Class

# This ofcourse won't work in Ruby 1.8.6 (universal-darwin9.0)
# and Rails version	2.3.2

# So we try this:

class ApplicationController < ActionController::Base

  require_dependency 'cart' # this solves the issue load :model
  require_dependency 'purchase' # this solves the issue load :model

end

By the way, for those who have this book, the official forum for it is: Beginning Ruby on Rails.

Learn more about require_dependency .

I hope it was useful.

Leave a comment