Forum: Ruby on Rails Help with hmabt relationships

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.
334ae822566107a2ea2b7a7cb88bd1d6?d=identicon&s=25 Brutyn Nick (Guest)
on 2006-01-23 20:19
(Received via mailing list)
hey,

I want to have this table structure with users and groups
A user can be in more than 1 group and a group has more than 1 user.

class User < ActiveRecord::Base
has_many_and_belongs_to :groups
end

class Group < ActiveRecord::Base
has_many_and_belongs_to :users
end

I was wondering if i can do this
for group in user.groups
 ...do stuff
end
-AND-
for user in group.users
 ...do stuff
end

I need both loops, can i do that, or do i need another structure...

Thanks in advance
Nick
51f10739fd903d3b57561a15e4598954?d=identicon&s=25 M Daggett (Guest)
on 2006-01-23 20:32
(Received via mailing list)
Yes you can do that fine. However, you may be missing a conceptual
model like "memberships". Not sure if you are using edge_rails but you
could have a user belong to a group through a membership. The groups
may contain users through their memberships. In this way you could
also capture other information (for example, most recent member, most
active member etc.)  that might fall through silently in a typical
HABTM.



On 1/23/06, Brutyn Nick <brutyn_nick@hotmail.com> wrote:
> has_many_and_belongs_to :users
>
>
--
This topic is locked and can not be replied to.