# A possibly complex find

**URL:** <https://www.ruby-forum.com/t/a-possibly-complex-find/189144>\
**Category:** Rails\
**Created:** [May 25, 2010, 2:39pm UTC](https://www.ruby-forum.com/t/a-possibly-complex-find/189144 "2010-05-25T14:39:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![slippy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/839c29/32.png) [@slippy](https://www.ruby-forum.com/u/slippy)\
**Post date:** [May 25, 2010, 2:39pm UTC](https://www.ruby-forum.com/t/a-possibly-complex-find/189144/1 "2010-05-25T14:39:19Z")

</div>

Hi, I am very new ruby on rails and I need to do what is I think is  
quite a complex find and I wanted to get some opinions on how to do it  
in a way that doesn’t hog resources.

My tables look like this :

CREATE TABLE “candidates” (“id” INTEGER PRIMARY KEY AUTOINCREMENT NOT  
NULL, “first\_name” varchar(255), “last\_name” varchar(255),  
“created\_at” datetime, “updated\_at” datetime);

CREATE TABLE “codes” (“id” INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,  
“name” varchar(255), “value” varchar(255), “created\_at” datetime,  
“updated\_at” datetime);

CREATE TABLE “candidates\_codes” (“code\_id” integer, “candidate\_id”  
integer);

candidates\_codes is my intersecting table between a habtm  
relationship

I have two groups of codes locations and languages and the code values  
are things like French, London, NY

I need to be able to search for candidates based on what language  
skills they have and in what location they are in.

The bit I really am struggling on is its needs to be able to work in  
two ways ether the candidate speaks German and lives in London and the  
second way is the candidate speaks German or lives in London.

I have been playing with find but I can only seem to start getting it  
to work by having loads of finds which is going to make the search  
slower as the number of candidates increases!

Thanks, Alex

---

<div class="post-metadata">

**Author:** ![slippy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/839c29/32.png) [@slippy](https://www.ruby-forum.com/u/slippy)\
**Post date:** [May 25, 2010, 3:10pm UTC](https://www.ruby-forum.com/t/a-possibly-complex-find/189144/2 "2010-05-25T15:10:59Z")

</div>

Your ‘codes’ smell ([Code smell - Wikipedia](http://en.wikipedia.org/wiki/Code_smell)) more like  
tags to me…

You should look at has\_many\_polymorphs, specifically the tagging  
feature.

I use it to great effect, allowing a search based on ALL or ANY of a set  
of entered tags.

---

<div class="post-metadata">

**Author:** ![slippy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/839c29/32.png) [@slippy](https://www.ruby-forum.com/u/slippy)\
**Post date:** [May 25, 2010, 4:23pm UTC](https://www.ruby-forum.com/t/a-possibly-complex-find/189144/3 "2010-05-25T16:23:33Z")

</div>

Thanks for your reply.

I see what you mean about tags, is it ok to use only stored tags which  
are grouped in different boxes ?

Locations

France  
German  
USA  
UK

Languages

French  
English

---

<div class="post-metadata">

**Author:** ![slippy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/839c29/32.png) [@slippy](https://www.ruby-forum.com/u/slippy)\
**Post date:** [May 25, 2010, 6:21pm UTC](https://www.ruby-forum.com/t/a-possibly-complex-find/189144/4 "2010-05-25T18:21:03Z")

</div>

My own usage was just that ‘a tag is a tag’, where yours seem to have  
distinct types or classes of tags (location or language).

If those are not editable by a user, then I could see managing the UI  
presentation of the tags so they could be separated. If they really are  
your tags, you can build the logic to know what is what (with two types,  
you only have to keep track of half the tags – if not type A then type  
B). You could use this knowledge to separate the tags for UI  
presentation and simply (re)combine them for persistence.

Taht would let you leverage the tagging portion of has\_many\_polymorphs.

Of course, that notion falls apart if the users can add their own tags.

---

<div class="post-metadata">

**Author:** ![slippy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/839c29/32.png) [@slippy](https://www.ruby-forum.com/u/slippy)\
**Post date:** [May 26, 2010, 1:30pm UTC](https://www.ruby-forum.com/t/a-possibly-complex-find/189144/5 "2010-05-26T13:30:17Z")

</div>

Thanks, I will give this a try!

Alex
