Skip to content

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

View the snippets archive ››

3 comments made

Eduardo commented on 14 Jul 08 at 08:39

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

Paul commented on 14 Jul 08 at 09:16

Indeed. But there are always edge cases.

Sickill commented on 28 Jul 08 at 03:07

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

Got something to say?

About

Paul is a web developer for Kyanmedia web agency. He's lucky enough to write in Ruby on Rails full-time and uses this site to post snippets of code.

Contact

my name at gmail.com

More snippets

Take a look in the archive

Need a website?

Contact my employer. Make sure to check out our portfolio of work.

Hosting

I recommend hostingrails.com