Active Record Associations


#1

I have tables:

users (id, name)
computers (id, name)
mobiltelefons (id, name)

OwnComputers (id, user_id, computer_id)
OwnMobiltelefons (id, user_id, mobiltelefon_id)

So is it has_many :through or has_and_belongs_to_many or what?

I only need to search one “own” table at time:
All computers that user “xx” owns OR
All Mobiltelefons that user “xx” owns OR
who owns mobilitefon “zz” OR
who owns computer “yy”

So…

class Computers < ActiveRecord::Base
has_many :OwnComputer
has_many :user, :through => :OwnComputer
end

class OwnComputers < ActiveRecord::Base
belongs_to :Computer
belongs_to :User
end

class Users < ActiveRecord::Base
???
end

class Mobiltelefons < ActiveRecord::Base
???
end

class OwnMobiltelefons < ActiveRecord::Base
???
end


#2

If I understand correctly what you need, a computer may belong to many
users and one user can have many computer, right? same for phones, I
assume.

In this case, the relationship is :has_and_belongs_to_many, but you do
not need the classes OwnComputer and the other one. you just need two
tables (without id) which will contain, for each record, a couple of
id identifying one coupling (user-computer for one table, user-phone
for the other).
have a look at:
http://wiki.rubyonrails.org/rails/pages/has_and_belongs_to_many
for naming conventions.

Regards,

Rey9999


#3

Rey9999 wrote:

If I understand correctly what you need, a computer may belong to many
users and one user can have many computer, right? same for phones, I
assume.

Yes

In this case, the relationship is :has_and_belongs_to_many, but you do

How can I find all users whose name is “James%” and their computers (not
mobiles)?

User.find(:all, ???)


#4

How can I find all users whose name is “James%” and their computers (not
mobiles)?

User.find(:all, ???)

Help ???


#5

Sijo Kg wrote:

I mean something like this:

SELECT users.name, computers.name FROM users, OwnComputers, computers
JOIN OwnComputers ON users.id = OwnComputers.users_id
JOIN computers ON OwnComputers.computers_id = computers.id
WHERE user.name LIKE “James%”


#6

Hi

@users = User.find(:all,:conditions=> [‘name LIKE ?’, “James%”])
@user_computers = []
@users.each do |user|
@user_computers << user.computers
end

@user_computers now contains computers you need

Sijo


#7

Or use has_many :through instead.

Blog: http://random8.zenunit.com/
Learn rails: http://sensei.zenunit.com/

On 11/02/2009, at 11:50 PM, Sijo Kg removed_email_address@domain.invalid


#8

Why is a good question here. In other words, what are you going to do
with the computers and users once
You get them.

Blog: http://random8.zenunit.com/
Learn rails: http://sensei.zenunit.com/

On 11/02/2009, at 4:42 AM, James B. <rails-mailing-list@andreas-


#9

Oh by the way if you do that you’ll end up with an array of arrays.
Might not be what you want.

Blog: http://random8.zenunit.com/
Learn rails: http://sensei.zenunit.com/

On 11/02/2009, at 11:50 PM, Sijo Kg removed_email_address@domain.invalid