# What is the RoR Way for this "find"?

**URL:** <https://www.ruby-forum.com/t/what-is-the-ror-way-for-this-find/145695>\
**Category:** Rails\
**Created:** [August 13, 2008, 12:24am UTC](https://www.ruby-forum.com/t/what-is-the-ror-way-for-this-find/145695 "2008-08-13T00:24:10Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![newrubygirl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e274bd/32.png) [@newrubygirl](https://www.ruby-forum.com/u/newrubygirl)\
**Post date:** [August 13, 2008, 12:24am UTC](https://www.ruby-forum.com/t/what-is-the-ror-way-for-this-find/145695/1 "2008-08-13T00:24:10Z")

</div>

I have 2 tables that currently are not joined with a has\_many or other  
relationship.

Table 1 has a list of records that are either active or not. Table 2  
has the related records with additional data. I need to check Table 1  
to see if the record is active and if it is, search Table 2 for records  
by ID.

@table1 = Model1.find(:all, :select =\> “ID”, :conditions =\> [“active =  
‘true’”])

@table2 = Model2.find(:all, :conditions =\> [“ID IN (#{@table1})”])

This doesn’t work so I’m looking to find the best RoR way that does  
work. Do I need to add a relationship between the tables? Also, what  
should these find statements look like? Do I do a join, an include,  
something else?

Thanks for the help.

---

<div class="post-metadata">

**Author:** ![newrubygirl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e274bd/32.png) [@newrubygirl](https://www.ruby-forum.com/u/newrubygirl)\
**Post date:** [August 13, 2008, 12:27am UTC](https://www.ruby-forum.com/t/what-is-the-ror-way-for-this-find/145695/2 "2008-08-13T00:27:58Z")

</div>

@table1 = Model1.find(:all, :select =\> “id, active”, :conditions =\>  
[“active = ?”, true], :include =\> “table2”)  
@table2 = @table1.table2

Please use “real” names in your examples as it helps us to provide  
more accurate information. For example, I don’t know what kind of  
association table2 has with table1, so I’ve treated it as a belongs\_to.

---

<div class="post-metadata">

**Author:** ![newrubygirl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e274bd/32.png) [@newrubygirl](https://www.ruby-forum.com/u/newrubygirl)\
**Post date:** [August 13, 2008, 12:44am UTC](https://www.ruby-forum.com/t/what-is-the-ror-way-for-this-find/145695/3 "2008-08-13T00:44:00Z")

</div>

Ryan B. wrote:

> Please use “real” names in your examples as it helps us to provide  
> more accurate information. For example, I don’t know what kind of  
> association table2 has with table1, so I’ve treated it as a belongs\_to.

Here’s what I have for my code.

Users  
Committees

I want to find all of the active users and then from there, get all of  
the committees that each person is on.

So do I need to set up the has\_many and belongs\_to relationships for  
these models?

Secondly, I am easily able to make the first piece of code work.

@user\_list = User.find(:all, :select =\> “id, active”, :conditions =\>  
[“active =\>”, true], :include =\> “committees”)

This gives me [#\<User id:1, Active: true\>, #\<User id:2, Active: true\>,  
#\<User id:3, Active: true\>]

But I’m not able to make the second part of this work to get the list of  
committees for each user.

Thanks!

---

<div class="post-metadata">

**Author:** ![newrubygirl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e274bd/32.png) [@newrubygirl](https://www.ruby-forum.com/u/newrubygirl)\
**Post date:** [August 13, 2008, 12:58am UTC](https://www.ruby-forum.com/t/what-is-the-ror-way-for-this-find/145695/4 "2008-08-13T00:58:26Z")

</div>

On Aug 12, 5:44 pm, Becca G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ## Thanks!
> 
> Posted viahttp://www.ruby-forum.com/.

You need to relate the tables in the database, as well as in Rails.

So make sure your committees table has a user\_id column. Then, a  
committee can belong to a user.

However, if you really have a many-to-many relationship, then a join  
table might work best. Check out the Rails API docs on the  
has\_many :through option for how to set that up. In short, you create  
new table that relates users to committees. So once you have a user  
object, you can just do user.committees.

Jeff

REST with Rails  
Oct 4, 2008, in Austin, Texas  
[http://www.purpleworkshops.com/workshops/rest-and-web-services](http://www.purpleworkshops.com/workshops/rest-and-web-services)

---

<div class="post-metadata">

**Author:** ![newrubygirl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e274bd/32.png) [@newrubygirl](https://www.ruby-forum.com/u/newrubygirl)\
**Post date:** [August 13, 2008, 12:59am UTC](https://www.ruby-forum.com/t/what-is-the-ror-way-for-this-find/145695/5 "2008-08-13T00:59:23Z")

</div>

> But I’m not able to make the second part of this work to get the list of  
> committees for each user.  
> Well, of course not. :include =\> “committees” is going to expect that  
> the two tables are associated, which as you mentioned they are not. How  
> is Rails supposed to find the “committees” association of you didn’t  
> specify it?

It sounds to me like the two tables (users and committees) SHOULD be  
associated anyway. Is there some reason they are not?

Becca G. wrote:

> Ryan B. wrote:
> 
> > Please use “real” names in your examples as it helps us to provide  
> > more accurate information. For example, I don’t know what kind of  
> > association table2 has with table1, so I’ve treated it as a belongs\_to.
> 
> Here’s what I have for my code.
> 
> Users  
> Committees
> 
> I want to find all of the active users and then from there, get all of  
> the committees that each person is on.
> 
> So do I need to set up the has\_many and belongs\_to relationships for  
> these models?
> 
> Secondly, I am easily able to make the first piece of code work.
> 
> @user\_list = User.find(:all, :select =\> “id, active”, :conditions =\>  
> [“active =\>”, true], :include =\> “committees”)
> 
> This gives me [#\<User id:1, Active: true\>, #\<User id:2, Active: true\>,  
> #\<User id:3, Active: true\>]
> 
> But I’m not able to make the second part of this work to get the list of  
> committees for each user.
> 
> Thanks!

---

<div class="post-metadata">

**Author:** ![newrubygirl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e274bd/32.png) [@newrubygirl](https://www.ruby-forum.com/u/newrubygirl)\
**Post date:** [August 13, 2008, 1:12am UTC](https://www.ruby-forum.com/t/what-is-the-ror-way-for-this-find/145695/6 "2008-08-13T01:12:13Z")

</div>

I have added

class User \<A ctiveRecord::Base  
has\_many :comittees  
end

class Comittee \< ActiveRecord::Base  
belongs\_to :user  
end

I am still unable to get the second part of the code to work.

@table2 = @table1.table2

@comittees = @user\_list.committees - is this the wrong syntax?

---

<div class="post-metadata">

**Author:** ![newrubygirl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e274bd/32.png) [@newrubygirl](https://www.ruby-forum.com/u/newrubygirl)\
**Post date:** [August 13, 2008, 1:45am UTC](https://www.ruby-forum.com/t/what-is-the-ror-way-for-this-find/145695/7 "2008-08-13T01:45:15Z")

</div>

In your has\_many you spelt “comittees” with one m, in your syntax you  
spelt it with two: “committees”

---

<div class="post-metadata">

**Author:** ![newrubygirl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e274bd/32.png) [@newrubygirl](https://www.ruby-forum.com/u/newrubygirl)\
**Post date:** [August 13, 2008, 3:26am UTC](https://www.ruby-forum.com/t/what-is-the-ror-way-for-this-find/145695/8 "2008-08-13T03:26:31Z")

</div>

Becca G. wrote:

> I have added
> 
> class User \<A ctiveRecord::Base  
> has\_many :comittees  
> end
> 
> class Comittee \< ActiveRecord::Base  
> belongs\_to :user  
> end

Should a committee be comprised of only one user? That’s what you have  
with this. If a committee is really comprised of n users, you need to  
use a has\_many :through. Off the side of my head, it’s something like

class User  
has\_many :committee\_members  
has\_many :committees, :through =\> :committee\_members  
end

class Committee  
has\_many :committee\_members  
has\_many :users, :through =\> :committee\_members  
end

class CommitteeMember  
belongs\_to :committee  
belongs\_to :user  
end

Then you can go both ways. You can get a list of committees for a  
particular user or a list of users for a particular committee. And,  
since it’s a has\_many :through, you can put attributes on  
committee\_members that relate specifically to that user’s membership in  
the committee (such as is\_chair or something).

Peace,  
Phillip
