# "map.resources :customers" and custom methods

**URL:** <https://www.ruby-forum.com/t/map-resources-customers-and-custom-methods/146131>\
**Category:** Rails\
**Created:** [August 18, 2008, 10:54am UTC](https://www.ruby-forum.com/t/map-resources-customers-and-custom-methods/146131 "2008-08-18T10:54:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tktktk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a3d4f5/32.png) [@tktktk](https://www.ruby-forum.com/u/tktktk)\
**Post date:** [August 18, 2008, 10:54am UTC](https://www.ruby-forum.com/t/map-resources-customers-and-custom-methods/146131/1 "2008-08-18T10:54:58Z")

</div>

Hi

We’re using map.resources :customers to give an external  
application access to our customers table. In addition to the  
standards methods like index/new/show etc… we would like to  
implement some custom methods.

For example one custom method could be the case when you want to  
return all customers with a specific lastname instead of just all  
customers or one specific customers.

So I implemented a method searchCustomers which takes the respective  
lastname param for searching the database. Now that method doesn’t  
work because rails tries to map the searchCustomer?  
customer[lastname]=xy to an ID.

Do I need to have a separate controller for these methods? or am I  
missing something

---

<div class="post-metadata">

**Author:** ![tktktk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a3d4f5/32.png) [@tktktk](https://www.ruby-forum.com/u/tktktk)\
**Post date:** [August 18, 2008, 10:58am UTC](https://www.ruby-forum.com/t/map-resources-customers-and-custom-methods/146131/2 "2008-08-18T10:58:29Z")

</div>

Use the :collection option in map.resources ([api.rubyonrails.org](http://api.rubyonrails.org)). And  
consider giving it a better name than searchCustomers.

Rein

---

<div class="post-metadata">

**Author:** ![tktktk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a3d4f5/32.png) [@tktktk](https://www.ruby-forum.com/u/tktktk)\
**Post date:** [August 18, 2008, 11:37am UTC](https://www.ruby-forum.com/t/map-resources-customers-and-custom-methods/146131/3 "2008-08-18T11:37:11Z")

</div>

> So I implemented a method searchCustomers which takes the respective  
> lastname param for searching the database. Now that method doesn’t  
> work because rails tries to map the searchCustomer?  
> customer[lastname]=xy to an ID.

I don’t know if you’d like to approach it the same way I did, but here  
goes.

I use the index method for my searches. I check if there are search  
params  
in the request, and modify my find accordingly.

Regards,  
Franz

---

<div class="post-metadata">

**Author:** ![tktktk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a3d4f5/32.png) [@tktktk](https://www.ruby-forum.com/u/tktktk)\
**Post date:** [August 18, 2008, 11:47am UTC](https://www.ruby-forum.com/t/map-resources-customers-and-custom-methods/146131/4 "2008-08-18T11:47:43Z")

</div>

On Mon, Aug 18, 2008 at 11:36 AM, Franz S.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I use the index method for my searches. I check if there are search params  
> in the request, and modify my find accordingly.

Me too. From a REST view it is fine to think about search as getting  
the collection of resources (index) scoped by a “search” param.

---

<div class="post-metadata">

**Author:** ![tktktk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a3d4f5/32.png) [@tktktk](https://www.ruby-forum.com/u/tktktk)\
**Post date:** [August 18, 2008, 11:52am UTC](https://www.ruby-forum.com/t/map-resources-customers-and-custom-methods/146131/5 "2008-08-18T11:52:59Z")

</div>

Agreed.
