RailsConf Europe 2006: Search with Rails

(slides)

Now at the shopping.nouvelobs.com ecommerce talk which is a large news site in France. Speaker is Gwendal Rou, very French.

ModelSearch is an extension of find that allows something like this:

search = ProductSearch.new()
search.buyable = true
search.Find(:all)

The idea is that searching with multiple columns is not at the usual high-level in Rails. You have to use SQL.

ModelSearch is capable of; Televisions costing less than 300, sorted by price.

search = ProductSearch.new
search.keyword - 'television'
search.max_price = 300
search.sort = :price
search.find(:all)

That is quite handy. I assume ‘max_’ is dynamically added onto model members. i.e. I assume you could have min_price too. My assumption is nearly correct, you just need to set it up in the model a bit.

A further extension lets you pass the search object into the normal model find e.g. Product.find(:all, search.find_options)

ProductSearch is a class you subclass from ModelSearch and setup with various params. e.g. search_key :keyword, :search => :keyword, in => [:name, :description]

All nicely model based this. Buyable, from the first code, is a custom method in the ProductSearch model. It works well with a form, letting you just pass in properly named input fields.

ModelSearch is planned for release soon.

All in all a nice bit of code for real world problems.

Viewing 4 Comments

Trackbacks

close Reblog this comment
blog comments powered by Disqus