# Help with DRY. I feel like im doing more then I have to.

**URL:** <https://discuss.rubyonrails.org/t/help-with-dry-i-feel-like-im-doing-more-then-i-have-to/3844>\
**Category:** rubyonrails-talk\
**Created:** [October 31, 2006, 8:19pm UTC](https://discuss.rubyonrails.org/t/help-with-dry-i-feel-like-im-doing-more-then-i-have-to/3844 "2006-10-31T20:19:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmazzi](https://avatars.discourse-cdn.com/v4/letter/j/b9e5f3/32.png) [@jmazzi](https://discuss.rubyonrails.org/u/jmazzi)\
**Post date:** [October 31, 2006, 8:19pm UTC](https://discuss.rubyonrails.org/t/help-with-dry-i-feel-like-im-doing-more-then-i-have-to/3844/1 "2006-10-31T20:19:21Z")

</div>

Throughout my Message controller, I do this a lot:

@message = Message.find(params[:id],:conditions =\> ["user\_id = ?", @session['user'].id])

Since you need to authenticate to use this app, is there a way i can tie the Accounts model in with the Message model so I dont have to pass in the user\_id everytime?

---

<div class="post-metadata">

**Author:** ![Pat\_Maddox](https://avatars.discourse-cdn.com/v4/letter/p/eb9ed0/32.png) [@Pat\_Maddox](https://discuss.rubyonrails.org/u/Pat_Maddox)\
**Post date:** [October 31, 2006, 9:11pm UTC](https://discuss.rubyonrails.org/t/help-with-dry-i-feel-like-im-doing-more-then-i-have-to/3844/2 "2006-10-31T21:11:50Z")

</div>

Do you have a helper method that provides the current user? Something like:

def current\_user &nbsp;&nbsp;@cur\_user ||= User.find(session[:user\_id]) end

Then set up a relationship in your user model: class User \< AR::Base &nbsp;&nbsp;has\_many :messages end

then you can do current\_user.messages.find(params[:id])

If you go through the messages relation, it constricts it only to those messages that have the user\_id belonging to that user.

Alternatively you could do Message.find\_by\_id\_and\_user\_id(params[:id], session[:user\_id])

or set up a method in your Message class.

I think setting up the has\_many :messages association and going through that would be the most idiomatic way.

Also use the accessor method session instead of the instance variable @session, which is deprecated.

Pat
