Using routes in models in Ruby on Rails

Posted on 13 July 2008

To use routes in your model just add the following to config/environment.rb...


class ActiveRecord::Base
 include ActionController::UrlWriter

 host = case ENV['RAILS_ENV']
 when "production"
   "yourlivedomain.com"
 when "development"
   "localhost:3000"
 end
 default_url_options[:host] = host
end

Comments left...

  • And there goes proper MVC encapsulation flying out of the window…

    Eduardo at 14 Jul 08 at 08:39

  • Indeed. But there are always edge cases.

    Paul at 14 Jul 08 at 09:16

  • I agree with Eduardo. If you need to use routes from your model then you probably have your application designed bad way.

    Sickill at 28 Jul 08 at 03:07

  • I agree with Eduardo as well.

    Zach Dennis at 17 Oct 08 at 17:13

Got something to say?