Searching across tables with Wice Grid in Rails

Say that in your Rails app’s admin interface you have a table of orders, each of which is associated with a customer. You need to find orders belonging to a particular customer, by e-mail address. This is a search across two tables, orders and customers.

Wice Grid, a fantastic data grid for Rails, makes this search easy. Check out our intro to Wice Grid if you haven’t already, then come back here for this trick.

In your controller, when you declare your grid, include the other table that you’re searching.

Now, in your view, when you define the column that contains the customer’s e-mail address, reference the model (Customer) and the field within that model (email).

Wice generates a search box above the customer e-mail column. Enter an e-mail address to find orders a associated with that customer.

Try out Wice Grid in your Rails app! It makes admin interfaces much easier to build.

For more background, check out our intro to Wice Grid.

If you’re building a broader search feature for your app’s users, check out our intro to full-text search in Rails. brings Rails job offers to you. Free for candidates. Salaries from $75,000 to $250,000. Sign up now!

2 thoughts on “Searching across tables with Wice Grid in Rails”

  1. However, with WiceGrid 3.4.0 Rails 4 produces the following deprecation message:

    DEPRECATION WARNING: It looks like you are eager loading table(s) (one of: tasks, statuses) that are referenced in a string SQL snippet. For example:

    Post.includes(:comments).where(“comments.title = ‘foo'”)

    Currently, Active Record recognizes the table in the string, and knows to JOIN the comments table to the query, rather than loading comments in a separate query. However, doing this without writing a full-blown SQL parser is inherently flawed. Since we don’t want to write an SQL parser, we are removing this functionality. From now on, you must explicitly tell Active Record when you are referencing a table from a string:

    Post.includes(:comments).where(“comments.title = ‘foo'”).references(:comments)

    This will be fixed in future versions of WiceGrid.

Leave a Reply