title: Can Rails Routing Helpers (i.e. mymodel_path(model)) be Used in Models?
body: <p>Say I have a Rails Model called Thing. Thing has a url attribute that can <strong>optionally</strong> be set to a URL somewhere on the Internet. In view code, I need logic that does the following:</p>
  
  <pre><code>&lt;% if thing.url.blank? %&gt;
  &lt;%= link_to('Text', thing_path(thing)) %&gt;
  &lt;% else %&gt;
  &lt;%= link_to('Text', thing.url) %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>This conditional logic in the view is ugly. Of course, I could build a helper function, which would change the view to this:</p>
  
  <pre><code>&lt;%= thing_link('Text', thing) %&gt;
  </code></pre>
  
  <p>That solves the verbosity problem, but I would really prefer having the functionality in the model itself. In which case, the view code would be:</p>
  
  <pre><code>&lt;%= link_to('Text', thing.link) %&gt;
  </code></pre>
  
  <p>This, obviously, would require a link method on the model. Here's what it would need to contain:</p>
  
  <pre><code>def link
    (self.url.blank?) ? thing_path(self) : self.url
  end
  </code></pre>
  
  <p>To the point of the question, thing_path() is an undefined method inside Model code. I'm assuming it's possible to "pull in" some helper methods into the model, but how? And is there a real reason that routing only operates at the controller and view layers of the app? I can think of lots of cases where model code may need to deal with URLs (integrating with external systems, etc).</p>