# Restul Controllers with multiple access points

**URL:** <https://discuss.rubyonrails.org/t/restul-controllers-with-multiple-access-points/13145>\
**Category:** rubyonrails-talk\
**Created:** [May 18, 2007, 10:29pm UTC](https://discuss.rubyonrails.org/t/restul-controllers-with-multiple-access-points/13145 "2007-05-18T22:29:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![blinking\_bear](https://avatars.discourse-cdn.com/v4/letter/b/a9a28c/32.png) [@blinking\_bear](https://discuss.rubyonrails.org/u/blinking_bear)\
**Post date:** [May 18, 2007, 10:29pm UTC](https://discuss.rubyonrails.org/t/restul-controllers-with-multiple-access-points/13145/1 "2007-05-18T22:29:04Z")

</div>

I’m trying to solve the same problem outlined in this blog post by Tore Darell. Basically, what’s the best practice for having multiple access points to a nested resource:

IE: class User \< ActiveRecord::Base

has\_many :posts end

class Category \< ActiveRecord::Base has\_many :posts end

class Post \< ActiveRecord::Base belongs\_to :user belongs\_to :category end

This blog points out one method but I feel like creating a ton of different controllers is the cleanest method, any other thoughts?

[http://tore.darell.no/entries/2-cleaning-up-your-nest-how-to-nest-resources-with-multiple-access-pointshttp://tore.darell.no/entries/2-cleaning-up-your-nest-how-to-nest-resources-with-multiple-access-points](http://tore.darell.no/entries/2-cleaning-up-your-nest-how-to-nest-resources-with-multiple-access-pointshttp://tore.darell.no/entries/2-cleaning-up-your-nest-how-to-nest-resources-with-multiple-access-points)

---

<div class="post-metadata">

**Author:** ![Geoff\_B](https://avatars.discourse-cdn.com/v4/letter/g/bbe5ce/32.png) [@Geoff\_B](https://discuss.rubyonrails.org/u/Geoff_B)\
**Post date:** [May 20, 2007, 5:14pm UTC](https://discuss.rubyonrails.org/t/restul-controllers-with-multiple-access-points/13145/2 "2007-05-20T17:14:46Z")

</div>

I would say, as long as there's more similarities than differences in the way you retrieve and present posts in the different contexts, stick to doing it DRY, i.e. one controller and one set of views for posts, and use private methods in the controller and helper methods in the view to abstract away the complexity. For example, you could do something like this in the controller:

class PostsController \< ApplicationController &nbsp;&nbsp;before\_filter :find\_parent

&nbsp;&nbsp;def index &nbsp;&nbsp;&nbsp;&nbsp;@posts = @parent ? @parent.posts.find(:all) : Post.find(:all) &nbsp;&nbsp;end

private &nbsp;&nbsp;def find\_parent &nbsp;&nbsp;&nbsp;&nbsp;@parent = &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if params[:category\_id] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Category.find(params[:category\_id]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elsif params[:user\_id] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;User.find(params[:user\_id]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end end
