I currently have the following models and am designing search functionality that searches within these 3 models: Locations Users User_friends

Should I create a search controller or should I put the action in one of the existing locations, users, or user_friends controllers? If so should it be pluralized - searches_controller or just search_controller?


Would depend on a few details about your search options. Our website has rather complex search options (using Apache Solr to search over articles that have a heavy load of facets like categories, technical properties etc.).

I have actually two search controllers, one for customers (basic search functionality) and one in the admin section that handles some more complicated internal queries.

The outward bound controller is named search_controller and has only a show action. Mostly because the text input that triggers the search is available from everywhere on the site, so you can do only a single "search". (I'm not 100% happy with this singular naming here and may change it)

The admin variant is called searches_controller and has an index action and a show action. The index action will display a page with all the search options. This then triggers the show action to display search results.

If there is any doubt, I would more likely go with the plural variation, it's just more simple. index giving the options, show displays the results. (though using new for the options would maybe make sense too)

Since you search over multiple tables a dedicated searches_controller most likely makes sense, though if one of the models is the "main" model, you could as well use this models index action if this isn't already taken. If you would start to add non REST actions, it's a sign that a controller makes sense (not mandatory of course)

The work load to actually search belongs in the models or some library.


I'd create a Searches controller, but perform the query in the models.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.