# Calling RESTful 'scoped' INDEX when params\[:parent.id\] is no

**URL:** <https://www.ruby-forum.com/t/calling-restful-scoped-index-when-params-parent-id-is-no/107064>\
**Category:** Rails\
**Created:** [June 17, 2007, 2:04pm UTC](https://www.ruby-forum.com/t/calling-restful-scoped-index-when-params-parent-id-is-no/107064 "2007-06-17T14:04:10Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 17, 2007, 2:04pm UTC](https://www.ruby-forum.com/t/calling-restful-scoped-index-when-params-parent-id-is-no/107064/1 "2007-06-17T14:04:10Z")

</div>

Jordon,  
I have a VERY hierchical database and am using a RESTful  
approach. Normally, when users’ enter the system they’ll cascade from  
level to level in the database and thus the hierchical information  
such as child.parent.grandchild\_id are always available to the RESTful  
routes.  
However, when I attempt to simply call a middle level table.index I  
obviously fail, because the parent\_id is not available to pass to the  
RESTful route.  
I’d like to have a ‘safety mechanism’ built into my code that handles  
this, and provide the pieces that describe the problem.  
Here is an example of my before\_filter event and the way I use it  
to call my index:

def find\_parent  
@find\_parent || @project = Project.find(params[:project\_id])  
end

Here is a typical table.index action:

def index  
@portal\_pages = Paginator.new self, @project.portals.count, 3,  
params[:page]  
@portals = @project.portals.find :all,  
:order =\> ‘description’,  
:limit =\> @portal\_pages.items\_per\_page,  
:offset =\> @portal\_pages.current.offset  
respond\_to do |format|  
format.html # index.rhtml  
format.xml { render :xml =\> @portals.to\_xml }  
end  
end

Would it be reasonable to make the INDEX code something like this;

def index  
if @project.nil?  
@portal\_pages = Paginator.new self, Portal.count, 3, params[:page]  
@portals = Portal.find :all,  
:order =\> ‘description’,  
:limit =\> @portal\_pages.items\_per\_page,  
:offset =\> @portal\_pages.current.offset  
else  
@portal\_pages = Paginator.new self, @project.portals.count, 3,  
params[:page]  
@portals = @project.portals.find :all,  
:order =\> ‘description’,  
:limit =\> @portal\_pages.items\_per\_page,  
:offset =\> @portal\_pages.current.offset  
end  
respond\_to do |format|  
format.html # index.rhtml  
format.xml { render :xml =\> @portals.to\_xml }  
end  
end

```
Fundumentally, what 'blows my mind' is that the 'find_parent'

```

‘before\_filter’ event ALWAYS has to fire and if no parent exists, I  
can’t see how to return a ‘@project.nil’ or whatever is needed to feed  
the if, else routine (above)?

Thank you,  
Kathy
