# Searching RESTful style

**URL:** <https://www.ruby-forum.com/t/searching-restful-style/103832>\
**Category:** Rails\
**Created:** [May 10, 2007, 8:39pm UTC](https://www.ruby-forum.com/t/searching-restful-style/103832 "2007-05-10T20:39:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rlwinter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecccb3/32.png) [@rlwinter](https://www.ruby-forum.com/u/rlwinter)\
**Post date:** [May 10, 2007, 8:39pm UTC](https://www.ruby-forum.com/t/searching-restful-style/103832/1 "2007-05-10T20:39:16Z")

</div>

Hello list,

Ok, first I would like to say I’m starting to get the hang of thinking  
the RESTful way. But, one thing that is still perplexing my  
conditioned brain is how to incorporate searching into the RESTful  
Rails style.

I’m sure this is a basic question, but I’d really like to see some  
examples what what people are doing in this regard.

So I understand that I have URLs like:

[http://example.com/posts](http://example.com/posts)  
[http://example.com/posts/123/comments](http://example.com/posts/123/comments)

But, what is the preferred technique for filtering those resource  
collections with conditions. I’m fairly sure you wouldn’t want to add  
a custom action like:

map.resource :posts, :collection =\> { :search =\> :get }

and add more for each type of search you want to perform.

I’m guessing that you would do something more dynamic inside the  
standard index action. Somehow I seem to have missed seeing any  
examples the RESTful way to do this.

Do we do something like:

[http://example.com/posts?publish\_date\_from=2007-05-01&publish\_date\_to=2007-05-15](http://example.com/posts?publish_date_from=2007-05-01&publish_date_to=2007-05-15)

And then determine within the index action if a query string exists  
and have conditions to deal with each different set of possible search  
criteria. I’m sure there’s a cleaner, easier way than that.

Any thoughts would be greatly appreciated,  
Robert W.

---

<div class="post-metadata">

**Author:** ![rlwinter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecccb3/32.png) [@rlwinter](https://www.ruby-forum.com/u/rlwinter)\
**Post date:** [May 10, 2007, 9:16pm UTC](https://www.ruby-forum.com/t/searching-restful-style/103832/2 "2007-05-10T21:16:22Z")

</div>

On Thu, May 10, 2007 at 11:38:35AM -0700, Robert W. wrote :

> criteria. I’m sure there’s a cleaner, easier way than that.  
> Personnaly, I would have a ‘search’ resource. To fetch results, you  
> simply GET from this resource. Back to your example, I would add routes  
> such as:

/search/posts/:publish\_date\_from/:publich\_date\_to/

and perform the search query in the show action.

You could extend this resource:

/search/:model/:criteria\*

Hope that helps,

–  
,========================.  
| Pierre-Alexandre M. |  
| email : removed\_email\_address@domain.invalid |  
`========================’

---

<div class="post-metadata">

**Author:** ![rlwinter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecccb3/32.png) [@rlwinter](https://www.ruby-forum.com/u/rlwinter)\
**Post date:** [May 11, 2007, 4:20pm UTC](https://www.ruby-forum.com/t/searching-restful-style/103832/3 "2007-05-11T16:20:31Z")

</div>

Pierre,

> Hope that helps,

Yes that does help quite a bit actually. What used to be about as  
clear as mud, is now about as clear as milk. I feel I’m close to  
seeing the solution, but still can’t quite see past the obstruction  
(err abstraction).

I’m still a bit of a novice with Rails, so I’m still struggling a bit  
on how to map out the routes and such. It would help to have a  
concrete example showing such things as the map.resources, a basic  
controller action for having a “Search” resource, etc. Could a  
“Search” resource be used generically to query any ActiveRecord  
subclasses?

---

<div class="post-metadata">

**Author:** ![rlwinter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecccb3/32.png) [@rlwinter](https://www.ruby-forum.com/u/rlwinter)\
**Post date:** [May 11, 2007, 6:20pm UTC](https://www.ruby-forum.com/t/searching-restful-style/103832/4 "2007-05-11T18:20:51Z")

</div>

On Fri, May 11, 2007 at 07:19:26AM -0700, Robert W. wrote :

> Yes that does help quite a bit actually. What used to be about as  
> clear as mud, is now about as clear as milk. I feel I’m close to  
> seeing the solution, but still can’t quite see past the obstruction  
> (err abstraction).

Cool!

> I’m still a bit of a novice with Rails, so I’m still struggling a bit  
> on how to map out the routes and such. It would help to have a  
> concrete example showing such things as the map.resources, a basic  
> controller action for having a “Search” resource, etc. Could a  
> “Search” resource be used generically to query any ActiveRecord  
> subclasses?

Sorry, you’re right: Show! Don’t Tell!

‘ruby script/generate scaffold\_resource Model’ will generated a lot of  
stuff.

If you need CRUD actions on a model mapped to a ‘real’ table in your  
database, don’t change anything! Notice the map.resources :model added  
to your routes.rb. It has generated for you the routes you need.  
Maybe you just want to tweak your controller, with a:  
before\_filter :admin\_authorize, :only =\> [:delete,:update]

You’ll have all the actions and views you need to CRUD your table. See  
your app/views/model/\*.rhtml for details.

What about the search resource? It’s a little more complicated (hey!  
it’s not a big thing! It’s Rails, not Struts!)…  
So we want to have an action mapped to a route like:  
/search/posts/:publish\_date\_from/:publish\_date\_to/

First, ‘ruby script/generate scaffold\_resource Search’ will do for you  
the half of work. But you still need to tweak the show action:

def show  
@results =  
Post.find\_by\_publish\_date\_from\_and\_publish\_date\_to(params[:publish\_date\_from],params[:publish\_date\_to])

```
respond_to do |format|
  format.html #do fancy stuff with @results in your view
  format.xml {}
end

```

end

Remove the DELETE and UPGRADE actions in this example, since you  
won’t need it. The INDEX one will be used to show a form for your  
customers  
to enter those informations  
(params[:publish\_date\_from],params[:publish\_date\_to]).

What about the routes? Remove the map.resources :searches since it’s not  
the type of routes you need. Instead, you could do something like:  
map.search ‘/search/posts/:publish\_date\_from/:publish\_date\_to’,  
:controller =\> ‘searches’,  
:action =\> ‘show’,  
:conditions =\> {:method =\> :get}

The search resource is actually a bad example since you only need a GET.  
For example, in a auth\_admins resource (wich will CRUD cookies for  
authorizations), /logout is mapped to the destroy action:  
def destroy  
session[:id] = nil

```
respond_to do |format|
  format.html { redirect_to auth_admins_url }
  format.xml { head :ok }
end

```

end  
map.cabinet\_logout ‘logout’, :controller =\> ‘auth\_cabinets’, :action  
=\> ‘destroy’, :conditions =\> {:method =\> :put}

Be careful to respect the ’ and :, since conventions for them in the  
routes module suck (erf, should I say: I don’t understand them?).

Actually, I’m not sure about the pluralization of ‘search’. Just add in  
your config/environment.rb:  
Inflector.inflections do |inflect|  
inflect.irregular ‘search’, ‘searches’  
end

Hope I’m clear,

–  
,========================.  
| Pierre-Alexandre M. |  
| email : removed\_email\_address@domain.invalid |  
`========================’
