Friday, January 09, 2009

Rails Select Filters that Map to In Queries

Example of a Rails select box that performs filtering and is backed by an IN query. First a constant is defined in the controller. It is an array of arrays where the "first" is the text to display in the select and the "last" is an array of the values the first represents.

Then we render that in our page with typical code:

Back in the controller, when the form is submitted, the following code sets up the conditions for a find call:

The only somewhat interesting bit is how the conditions for "Other" are set up with an inject from the valid options defined in the SERVICE_GRADE_CODE_OPTIONS constant.

1 comment:

  1. I have to say, I think you should move this into the model, under a custom search or find method. If you're talking about conditions (as in, finder conditions) it's better in the model.