Forum: Ruby on Rails Active Record Associations

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Fresh M. (Guest)
on 2009-02-10 16:31
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
Rey9999 (Guest)
on 2009-02-10 18:54
(Received via mailing list)
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_be...
for naming conventions.

Regards,

Rey9999
Fresh M. (Guest)
on 2009-02-10 19:42
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, ??????)
Fresh M. (Guest)
on 2009-02-11 10:32
> How can I find all users whose name is "James%" and their computers (not
> mobiles)?
>
> User.find(:all, ??????)

Help ???
Sijo k. (Guest)
on 2009-02-11 14:50
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
Fresh M. (Guest)
on 2009-02-11 15:45
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%"
Julian L. (Guest)
on 2009-02-11 15:59
(Received via mailing list)
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>
Julian L. (Guest)
on 2009-02-11 16:00
(Received via mailing list)
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>
Julian L. (Guest)
on 2009-02-11 17:26
(Received via mailing list)
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-
This topic is locked and can not be replied to.