# User search thru a database using the REST approach

**URL:** <https://www.ruby-forum.com/t/user-search-thru-a-database-using-the-rest-approach/131725>\
**Category:** Rails\
**Created:** [March 12, 2008, 7:23pm UTC](https://www.ruby-forum.com/t/user-search-thru-a-database-using-the-rest-approach/131725 "2008-03-12T19:23:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![liquidrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a6a055/32.png) [@liquidrails](https://www.ruby-forum.com/u/liquidrails)\
**Post date:** [March 12, 2008, 7:23pm UTC](https://www.ruby-forum.com/t/user-search-thru-a-database-using-the-rest-approach/131725/1 "2008-03-12T19:23:03Z")

</div>

How can you implement a search function using the REST approach? Is  
there an example of this somewhere on the web? Thanks!

---

<div class="post-metadata">

**Author:** ![liquidrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a6a055/32.png) [@liquidrails](https://www.ruby-forum.com/u/liquidrails)\
**Post date:** [March 13, 2008, 1:13am UTC](https://www.ruby-forum.com/t/user-search-thru-a-database-using-the-rest-approach/131725/2 "2008-03-13T01:13:48Z")

</div>

It would seem rather easy as far as you have two main options:

1. Think of the “search” itself as a resource
2. Think of the “search” as a condition of “index” action

For one, create the SearchController which will manage “search”  
resources if you will. New will be the form to enter the search query,  
and “create” will perform the search and return the results, etc.

For two, simply have the “index” action accept optional query  
conditions. So, by default, index would display all the items and then  
by specifying constraints, this could also act as a search of sorts.

Hope that helps.

---

<div class="post-metadata">

**Author:** ![liquidrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a6a055/32.png) [@liquidrails](https://www.ruby-forum.com/u/liquidrails)\
**Post date:** [March 13, 2008, 5:38pm UTC](https://www.ruby-forum.com/t/user-search-thru-a-database-using-the-rest-approach/131725/3 "2008-03-13T17:38:10Z")

</div>

Thanks, Nathan! I think I’m finally getting the hang of this REST  
approach and am starting to like it. Urls are so much cleaner and it’s  
starting to make sense! Cheri

---

<div class="post-metadata">

**Author:** ![liquidrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a6a055/32.png) [@liquidrails](https://www.ruby-forum.com/u/liquidrails)\
**Post date:** [March 13, 2008, 9:28pm UTC](https://www.ruby-forum.com/t/user-search-thru-a-database-using-the-rest-approach/131725/4 "2008-03-13T21:28:46Z")

</div>

The search will be needed for just one model, so I will go with option  
2. I also plan to include the search terms in the url for the search  
results view- which I assume under the REST model would be the search  
“show” view - so that the user can use the Back button to get back to  
the search results page. - Cheri

AndyV wrote:

> Good suggestion, Nathan. I think the main question (for Cheri) to  
> answer is whether Search is something that is going to be needed for  
> many different models or simply this one. If it’s something that will  
> be done for many things and you need to render a consistent list then  
> go with 1; if it’s primarily one (or two) models then go with 2.
> 
> On Mar 12, 8:13 pm, Nathan E. \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![liquidrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a6a055/32.png) [@liquidrails](https://www.ruby-forum.com/u/liquidrails)\
**Post date:** [March 13, 2008, 5:42pm UTC](https://www.ruby-forum.com/t/user-search-thru-a-database-using-the-rest-approach/131725/5 "2008-03-13T17:42:59Z")

</div>

Good suggestion, Nathan. I think the main question (for Cheri) to  
answer is whether Search is something that is going to be needed for  
many different models or simply this one. If it’s something that will  
be done for many things and you need to render a consistent list then  
go with 1; if it’s primarily one (or two) models then go with 2.

On Mar 12, 8:13 pm, Nathan E. \<rails-mailing-l…@andreas-
