# Restul Controllers with multiple access points

**URL:** <https://www.ruby-forum.com/t/restul-controllers-with-multiple-access-points/104530>\
**Category:** Rails\
**Created:** [May 19, 2007, 12:30am UTC](https://www.ruby-forum.com/t/restul-controllers-with-multiple-access-points/104530 "2007-05-19T00:30:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![blinking\_bear](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a9a28c/32.png) [@blinking\_bear](https://www.ruby-forum.com/u/blinking_bear)\
**Post date:** [May 19, 2007, 12:30am UTC](https://www.ruby-forum.com/t/restul-controllers-with-multiple-access-points/104530/1 "2007-05-19T00:30:33Z")

</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:** ![blinking\_bear](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a9a28c/32.png) [@blinking\_bear](https://www.ruby-forum.com/u/blinking_bear)\
**Post date:** [May 20, 2007, 7:15pm UTC](https://www.ruby-forum.com/t/restul-controllers-with-multiple-access-points/104530/2 "2007-05-20T19:15:53Z")

</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  
before\_filter :find\_parent

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

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