Collection & Member Methods within a Route

by Melvin Ram

What’s the difference between collection & member methods inside a Rails route? This conversation should help you understand:

RubyHunt: What’s the use of the :collection and :member methods within a route? What’s the difference between the two?

melvinram: Collection adds routes to the entire collection

RubyHunt: So the collection is used to add another action inside the controller

melvinram: If you had a resource of Contacts, a collection route you might add would be delete_all_older_than_6_months

melvinram: and the route to get to it would be be /contacts/delete_all_older_than_6_months/

RubyHunt: Which otherwise would be restricted to only 7 actions ?

melvinram: yes

melvinram: A member method applies only to one record. For example, you might add a member method called upgrade_to_vip that would update one record to have a status of VIP or something…

melvinram: And the route would be /contacts/1/upgrade_to_vip

RubyHunt: So the :collection is used to add action inside a RESTful controller and the :member would be used to apply action for individual object.

melvinram: you got it!

Leave a Comment