# Ranges in RESTful resources

**URL:** <https://www.ruby-forum.com/t/ranges-in-restful-resources/119177>\
**Category:** Rails\
**Created:** [November 4, 2007, 11:41am UTC](https://www.ruby-forum.com/t/ranges-in-restful-resources/119177 "2007-11-04T11:41:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Evgeni\_Dobrev](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/edb3f5/32.png) [@Evgeni\_Dobrev](https://www.ruby-forum.com/u/Evgeni_Dobrev)\
**Post date:** [November 4, 2007, 11:41am UTC](https://www.ruby-forum.com/t/ranges-in-restful-resources/119177/1 "2007-11-04T11:41:59Z")

</div>

Hi,

I am writing an application with nested resources. I have a resource  
called Facilities which has\_many Reservations.

map.resources :facilities do |facilities|  
facilities.resources :reservations  
end

I have thus urls like

/facilities/1/reservations  
/facilities/1/reservations/4

I have the possibility to show a list of all reservations - the index  
action, and a particular reservation - the  
show action. How do I list all reservations between 10. April 10:00 and  
12. April 20:00 ?

How do I list ranges in RESTful resources?

Cheers,

evgeni

---

<div class="post-metadata">

**Author:** ![Evgeni\_Dobrev](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/edb3f5/32.png) [@Evgeni\_Dobrev](https://www.ruby-forum.com/u/Evgeni_Dobrev)\
**Post date:** [November 4, 2007, 2:31pm UTC](https://www.ruby-forum.com/t/ranges-in-restful-resources/119177/2 "2007-11-04T14:31:38Z")

</div>

Add a select (you can probably think of a better name) action to the  
reservations\_controller and pass the start date and end date to it  
through the URL?

For example:

In routes.rb:

map.resources :facilities do |facilities|  
facilities.resources :reservations, :collection =\> { :select =\> :get }  
end

In your views:

\<%= link\_to select\_facility\_resources(@facility, :start\_date =\>  
@start\_date, :end\_date =\> @end\_date) -%\>

Might want to change the format of the dates you pass in the URL.

In reservations\_controller:

# GET /facilities/1/reservations/select

# GET /facilities/1/reservations/select.xml

def select  
…

# Find the reservations using params[:start\_date] and

params[:end\_date]

# Also might want to perform some checking on these dates (encapsulate

in Reservation model?)

@reservations = @facility.reservations.find…(…)

respond\_to do |format|  
format.html { render :action =\> :index } # Or use select.html.erb  
format.xml { render :xml =\> @reservations }  
end  
end

An other option is to simply re-use the index action (rather than a  
separate action) for this, and check in there whether a date range is  
passed in. If so, find the reservations based on that range, otherwise  
default to finding all reservations. Not sure which option is more  
elegant.

---

<div class="post-metadata">

**Author:** ![Evgeni\_Dobrev](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/edb3f5/32.png) [@Evgeni\_Dobrev](https://www.ruby-forum.com/u/Evgeni_Dobrev)\
**Post date:** [November 5, 2007, 8:55am UTC](https://www.ruby-forum.com/t/ranges-in-restful-resources/119177/3 "2007-11-05T08:55:02Z")

</div>

Hi Gerjan,

thank you for your proposal. Sounds really  
streightforward. Is this approach along the RESTful lines? I have read  
the following discussion:

[http://groups.google.ca/group/rubyonrails-talk/browse\_thread/thread/e1759be395695179/2cd53b5742b7bec2](http://groups.google.ca/group/rubyonrails-talk/browse_thread/thread/e1759be395695179/2cd53b5742b7bec2)

I have been thinking about my problem for a couple of days now and I  
still think there should be a simpler solution. Maybe ranges should be  
treated as another resource. But then they share the same set of data  
with the index action. Sounds like a “2 Controller-one Model-thing” DHH  
was talking on the Eurocon about. Hmm …

Cheers,

evgeni
