views:

93

answers:

2

I have a application which stores documents created by users. These documents have authors, creation dates. The authors can have roles. Additionally a given document may get tagged with keywords. There are ~16K documents in the database, and a user may want to view the documents with any combination of this information as a limit. For example, see all documents by a given author, or published in a given time window, or from authors in this role, etc (and any combination of these).

Is there a recommended best way to implement this? In general I have passed the desired conditions in the params hash and then either used a complicated if ... elsif.... else.. to decode the passed conditions directly into different Model.find calls. Alternatively, I will use a similar if sequence to construct the proper SQL conditions phrase and then use a single find call.

Both of these seem like hacks and there should be a better way to do it with named scopes, but I can't figure out a clean way to deal with the permutations.

+1  A: 

Check out Ben Johnson's SearchLogic -- it's a gem that basically generates named scopes on the fly.

http://github.com/binarylogic/searchlogic/tree/master

John Pignata
A: 

You only need to have one line of code with Model.find. Instead of using conditional logic to create multiple find calls use conditional logic to populate the :conditions hash which is passed into the find call.

Charles Finkel