Using routes in models in Ruby on Rails
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
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
3 comments made
And there goes proper MVC encapsulation flying out of the window…
Indeed. But there are always edge cases.
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?