What is the RoR Way for this "find"?

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.

@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.

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!

On Aug 12, 5:44 pm, Becca G. [email protected]
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

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!

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs