# Using route resource in the model

**URL:** <https://www.ruby-forum.com/t/using-route-resource-in-the-model/127254>\
**Category:** Rails\
**Created:** [January 27, 2008, 4:26am UTC](https://www.ruby-forum.com/t/using-route-resource-in-the-model/127254 "2008-01-27T04:26:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chubbs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/47e85d/32.png) [@Chubbs](https://www.ruby-forum.com/u/Chubbs)\
**Post date:** [January 27, 2008, 4:26am UTC](https://www.ruby-forum.com/t/using-route-resource-in-the-model/127254/1 "2008-01-27T04:26:00Z")

</div>

How would i go about using a mapped resource in my model?

eg.

# routes file

map.resources :questions

# model

def after\_create  
notification = UserNotification.new(:user =\> current\_user)  
notification.body = ‘You posted a new question’  
notification.save  
end

comes up saying undefined method for question\_url

---

<div class="post-metadata">

**Author:** ![Chubbs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/47e85d/32.png) [@Chubbs](https://www.ruby-forum.com/u/Chubbs)\
**Post date:** [January 27, 2008, 2:35pm UTC](https://www.ruby-forum.com/t/using-route-resource-in-the-model/127254/2 "2008-01-27T14:35:43Z")

</div>

On 1/26/08, Chubbs [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> notification.body = 'You posted a new <a href="' +
> 
> ```
> 
> question\_url(question.id) + ‘"\>question’  
> notification.save  
> end
> 
> comes up saying undefined method for question\_url

Well, since url helpers are UI constructs, they really shouldn’t be  
used directly by models. They are available in controllers and views.

I’d humbly suggest that the best way to solve this is to have the  
UserNotification have a reference to the question, and then use  
question\_url in UserNotificationsController and/or it’s views.

This would properly keep the M-VC business logic-ui separation.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![Chubbs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/47e85d/32.png) [@Chubbs](https://www.ruby-forum.com/u/Chubbs)\
**Post date:** [January 27, 2008, 3:41pm UTC](https://www.ruby-forum.com/t/using-route-resource-in-the-model/127254/3 "2008-01-27T15:41:24Z")

</div>

mmm ok thanks the help
