Forum: Ruby on Rails Easy searching by hierarchical category...

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
adamfowleruk (Guest)
on 2009-01-13 11:29
(Received via mailing list)
I recently wanted to come up with a solution to searching by a
category (E.g. Shoes), sub category (E.g. Shoes > Blue), or sub sub
category to any depth. This represents an obvious DB challenge not to
mention model objects in RoR. I've come up with an elegant solution
using Binary Trees and thought it might be useful to others. I've
documented it here:

http://web.me.com/adamfowleruk/adamslife/Blog/Entr...

Note I've not used it in anger yet, and have not QA'ed the Ruby and
Java code at the end of the entry, but the approach is simple enough
and easy for anyone to pick up.

Category searching to any depth using 1 column/model variable. 8o)

I'd appreciate any feedback, especially if anyone can think of a more
elegant way to do it?

If anyone knows of where best to place the re-implementation of the
sqlite3 LIKE function in a RoR project I'd be grateful. I'll likely
use it elsewhere too. (I may infact implement REGEXP rather than re-
implement LIKE).

Regards,

Adam.
Mark Reginald J. (Guest)
on 2009-01-13 20:02
(Received via mailing list)
adamfowleruk wrote:
> Java code at the end of the entry, but the approach is simple enough
> and easy for anyone to pick up.
>
> Category searching to any depth using 1 column/model variable. 8o)
>
> I'd appreciate any feedback, especially if anyone can think of a more
> elegant way to do it?

Adam, the usual way this is done is with the nested-set data
structure, which effectively also uses btrees in the DB index
for the "left" column. Searching by pattern match would be
somewhat slower than such indexed nested-set searches.

Your method does have the advantage of being able to add children
without having to fix-up the tree afterwards, and for giving each
node a permanent hierarchy identifier that makes it easy to do
subcategory searches in search-engine indices. The nested-set
method can however be tweaked to have the same properties.

--
Rails Wheels - Find Plugins, List & Sell Plugins -
http://railswheels.com
This topic is locked and can not be replied to.