# Find/sql help - I'm confused

**URL:** <https://www.ruby-forum.com/t/find-sql-help-im-confused/150262>\
**Category:** Rails\
**Created:** [October 6, 2008, 6:18pm UTC](https://www.ruby-forum.com/t/find-sql-help-im-confused/150262 "2008-10-06T18:18:36Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Iain\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/6bbea6/32.png) [@Iain\_A](https://www.ruby-forum.com/u/Iain_A)\
**Post date:** [October 6, 2008, 6:18pm UTC](https://www.ruby-forum.com/t/find-sql-help-im-confused/150262/1 "2008-10-06T18:18:36Z")

</div>

Hello,

Somehow I just can’t seem to organise this in my head. So far I have  
three tables/models.

Group  
id

GroupCategory  
id, name

GroupCategoryValue  
group\_category\_id  
group\_id  
value

So a group has many group\_category\_values which hold values for the  
categories which the group is associated with.

What I want to do is:

Find all groups which, given an hash of (group\_category\_id’s and  
values) have every group\_category and every value.

For example

Group

(:id =\> 1, :name =\> Iain’s group)  
(:id =\> 2, :name =\> Another group)

GroupCategories  
(:id =\> 1, :name =\> Category1)  
(:id =\> 2, :name =\> Category2)  
(:id =\> 3, :name =\> Category3)

GroupCategoryValues  
(:group\_id =\> 1, :group\_category\_id =\> 1, :value =\> “hello”)  
(:group\_id =\> 1, :group\_category\_id =\> 2, :value =\> “my”)  
(:group\_id =\> 1, :group\_category\_id =\> 3, :value =\> “world”)

(:group\_id =\> 2, :group\_category\_id =\> 1, :value =\> “hello”)  
(:group\_id =\> 2, :group\_category\_id =\> 2, :value =\> “my”)  
(:group\_id =\> 2, :group\_category\_id =\> 3, :value =\> “mum”)

So if I gave the method {1 =\> “hello”, 2 =\> “my”} both groups 1 and 2  
should be returned  
but if I gave the method {1 =\> “hello”, 2 =\> “my”, 3 =\> “mum”} only  
group 2 should be returned.

Any ideas how to do this nicely? I tried using several methods and  
just got confused.

Also, the reason I want to do it in an sql/find way is because the  
resulting groups need paginating with the will\_paginate plugin?

hope you can help

Iain
