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

About Paul

Paul works for Kyan web design agency in Surrey, UK as a Ruby on Rails developer.

Follow Paul on Twitter

Email: paulsturgess [at] gmail.com

Read more articles in the archive →

Comments...

  • 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

  • So I'm generating a CSV report of all users in a system, and they want a link to the users admin show page... I need to roll that out of my model now, because it's not enough of an edge case? I think not.

    frank at 24 Aug 10 at 20:20

  • I like how the idiots come out with these "universal rules" which don't cover situations you'll find in a place like the real world.

    frank's example is a good one.

    If you're going to generate short links to your application's pages using a third-party service, then generating the URL to your resource to submit to the shortening service and saving the shortened URL in the model totally makes sense... otherwise you'd be generating a new short URL for the same resource every time the resource is loaded.

    Even if the service was smart enough to return an identical shortened URL then putting that stuff in a helper (instead of calling it once from within the model then storing the result) will yield a request to the shortening service on every load.

    I guess saving hundreds of thousands of requests to an external service is bad application design.

    Keep your mouths shut unless you have something to say, dittoheads.

    Dan at 10 Jan 11 at 16:34

  • Awesome post! This no longer works in Rails 3, but it got me on the right track. For the how to do this in Rails 3 you can go here:
    http://slaive-prog.tumblr.com/post/7618787555/using-routes-in-your-model-in-rails-3-0-x

    Sean at 14 Jul 11 at 10:37

  • Sorry, the link is here:
    <a href="http://slaive-prog.tumblr.com/post/7618787555/using-routes-in-your-model-in-rails-3-0-x">http://slaive-prog.tumblr.com/post/7618787555/using-routes-in-your-model-in-rails-3-0-x</a>

    Sean at 14 Jul 11 at 10:40

  • Thanks Paul. Just what I was looking for.

    My use case is a dashboard that has "action" items. Each action item has an interface that mirrors active record so that my front end devs can use an each method for a collection of action items. Each action item has a link however this is dynamic as each item is different.

    How else would I do this Eduardo? Turns out you have not seen every programming situation like you think you have. Sorry but your comment really annoyed me.

    Stewart Matheson at 29 Aug 11 at 00:30

Got something to say?