What is the RoR Way for this "find"?

Ryan Bigg 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!

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

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 Girl wrote:

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?

In your has_many you spelt "comittees" with one m, in your syntax you spelt it with two: "committees"

Becca Girl 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