# Help: self referential & search

**URL:** <https://www.ruby-forum.com/t/help-self-referential-search/181321>\
**Category:** Rails\
**Created:** [January 9, 2010, 4:07pm UTC](https://www.ruby-forum.com/t/help-self-referential-search/181321 "2010-01-09T16:07:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [January 9, 2010, 4:07pm UTC](https://www.ruby-forum.com/t/help-self-referential-search/181321/1 "2010-01-09T16:07:14Z")

</div>

hi, i have a category and a category\_type model, where category is  
selfreferential with parent\_id. now, im not 100% sure whats the best  
way for later purposes in terms of search etc., but my initial  
intention was the store the category\_types\_id only in the root (where  
parent\_id == 0), but like i said, how would i search through all  
children with a specific type of category\_type? i mean i could read  
them into an array, but thats just wrong.  
should i store the category\_type\_id in each category?

i hope that was not to unclear… 😉

thx tom

---

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [January 11, 2010, 6:26pm UTC](https://www.ruby-forum.com/t/help-self-referential-search/181321/2 "2010-01-11T18:26:17Z")

</div>

Tom T. wrote:

> hi, i have a category and a category\_type model, where category is  
> selfreferential with parent\_id. now, im not 100% sure whats the best  
> way for later purposes in terms of search etc., but my initial  
> intention was the store the category\_types\_id only in the root (where  
> parent\_id == 0), but like i said, how would i search through all  
> children with a specific type of category\_type? i mean i could read  
> them into an array, but thats just wrong.  
> should i store the category\_type\_id in each category?
> 
> i hope that was not to unclear… 😉
> 
> thx tom

You _could_ store it in another table, using has\_and\_belongs\_to\_many or  
has\_many :through …

---

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [January 11, 2010, 6:31pm UTC](https://www.ruby-forum.com/t/help-self-referential-search/181321/3 "2010-01-11T18:31:49Z")

</div>

thx, i went ahead and just marked the root - will see when i hot the  
forst roadblock;-)

tom

---

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [January 11, 2010, 6:33pm UTC](https://www.ruby-forum.com/t/help-self-referential-search/181321/4 "2010-01-11T18:33:29Z")

</div>

Tom T. wrote:

> hi, i have a category and a category\_type model, where category is  
> selfreferential with parent\_id. now, im not 100% sure whats the best  
> way for later purposes in terms of search etc.,

awesome\_nested\_set, of course. 🙂

> but my initial  
> intention was the store the category\_types\_id only in the root (where  
> parent\_id == 0), but like i said,

No, you didn’t say.

> how would i search through all  
> children with a specific type of category\_type? i mean i could read  
> them into an array, but thats just wrong.  
> should i store the category\_type\_id in each category?
> 
> i hope that was not to unclear… 😉

It was. What exactly are you trying to do?

> thx tom

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [January 11, 2010, 7:19pm UTC](https://www.ruby-forum.com/t/help-self-referential-search/181321/5 "2010-01-11T19:19:50Z")

</div>

Hi Marnen, the idea was having a selreferential table via parent\_id or  
lft/rgt to haev children etc… now my point of doubts was whether to  
make "each"top-level record (aka a category) root r to move it into a  
different table and mark there the type of that category…eg

categories:  
babysitting  
yard  
electrics

category\_types:  
service  
contractor  
job

hope that makes it clearer.  
tom

On Mon, Jan 11, 2010 at 12:33 PM, Marnen Laibow-Koser
