Ranges in RESTful resources

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

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.

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

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