# Help with hmabt relationships

**URL:** <https://www.ruby-forum.com/t/help-with-hmabt-relationships/53042>\
**Category:** Rails\
**Created:** [January 23, 2006, 8:19pm UTC](https://www.ruby-forum.com/t/help-with-hmabt-relationships/53042 "2006-01-23T20:19:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brutyn\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5f8ce5/32.png) [@Brutyn\_N](https://www.ruby-forum.com/u/Brutyn_N)\
**Post date:** [January 23, 2006, 8:19pm UTC](https://www.ruby-forum.com/t/help-with-hmabt-relationships/53042/1 "2006-01-23T20:19:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Brutyn\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5f8ce5/32.png) [@Brutyn\_N](https://www.ruby-forum.com/u/Brutyn_N)\
**Post date:** [January 23, 2006, 8:32pm UTC](https://www.ruby-forum.com/t/help-with-hmabt-relationships/53042/2 "2006-01-23T20:32:06Z")

</div>

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 N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> has\_many\_and\_belongs\_to :users

–
