# Tricky model situatione

**URL:** <https://www.ruby-forum.com/t/tricky-model-situatione/188579>\
**Category:** Rails\
**Created:** [May 14, 2010, 7:51am UTC](https://www.ruby-forum.com/t/tricky-model-situatione/188579 "2010-05-14T07:51:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![badnaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e36b37/32.png) [@badnaam](https://www.ruby-forum.com/u/badnaam)\
**Post date:** [May 14, 2010, 7:51am UTC](https://www.ruby-forum.com/t/tricky-model-situatione/188579/1 "2010-05-14T07:51:26Z")

</div>

I have two models Club and users. users are regular authenticated  
users and some of them might create a club. A club can also have  
members (essentially users) and there are attributes and models that  
apply to member but not to users.

Here is what I have right now.

Club =\> belongs\_to :user

User =\> has\_many clubs (since a user can host multiple clubs).

Now how do I fit this member model into the picture, a club can have  
many member, but since a club only has one user(the user who owns the  
club), I can’t really do a Club =\> has\_many :users. do i need to  
create a different model named ClubOwner for users who host clubs or  
is there a better way to do this.

Thanks for reading and helping!

---

<div class="post-metadata">

**Author:** ![badnaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e36b37/32.png) [@badnaam](https://www.ruby-forum.com/u/badnaam)\
**Post date:** [May 14, 2010, 10:21am UTC](https://www.ruby-forum.com/t/tricky-model-situatione/188579/2 "2010-05-14T10:21:55Z")

</div>

On 14 May 2010 06:50, badnaam [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Now how do I fit this member model into the picture, a club can have  
> many member, but since a club only has one user(the user who owns the  
> club), I can’t really do a Club =\> has\_many :users. do i need to  
> create a different model named ClubOwner for users who host clubs or  
> is there a better way to do this.

You can use something like  
Club has\_many :members, :class\_name =\> ‘User’, :foreign\_key =\>  
‘club\_membership\_id’  
User belongs\_to :club, :foreign\_key =\> ‘club\_membership\_id’

However that will only allow a user to be a member of one club, so  
probably you need a membership joins table so that you can have a club  
with many members and users in many clubs, each through the  
memberships table. The rails guide on activeRecord associations will  
help you.

Colin

---

<div class="post-metadata">

**Author:** ![badnaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e36b37/32.png) [@badnaam](https://www.ruby-forum.com/u/badnaam)\
**Post date:** [May 14, 2010, 5:47pm UTC](https://www.ruby-forum.com/t/tricky-model-situatione/188579/3 "2010-05-14T17:47:58Z")

</div>

On May 14, 1:50 am, badnaam [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Now how do I fit this member model into the picture, a club can have  
> many member, but since a club only has one user(the user who owns the  
> club), I can’t really do a Club =\> has\_many :users. do i need to  
> create a different model named ClubOwner for users who host clubs or  
> is there a better way to do this.

Assuming that a User can belong to many Clubs, you’ll need a join  
table between the two:

class User \< AR::Base  
has\_many :owned\_clubs, :foreign\_key =\> ‘owner\_id’, :class\_name =\>  
‘Club’  
has\_many :club\_memberships, :dependent =\> :destroy  
has\_many :clubs, :through =\> :club\_memberships  
end

class ClubMembership \< AR::Base  
belongs\_to :user  
belongs\_to :club  
end

class Club \< AR::Base  
has\_many :club\_memberships, :dependent =\> :destroy  
has\_many :users, :through =\> :club\_memberships  
belongs\_to :owner, :class\_name =\> ‘User’  
end

You’ll need to move your existing ‘user\_id’ column on Club to  
‘owner\_id’.

Hope this helps!

–Matt J.

---

<div class="post-metadata">

**Author:** ![badnaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e36b37/32.png) [@badnaam](https://www.ruby-forum.com/u/badnaam)\
**Post date:** [May 15, 2010, 4:11am UTC](https://www.ruby-forum.com/t/tricky-model-situatione/188579/4 "2010-05-15T04:11:20Z")

</div>

I am guessing I will need to change the routes like…

map.resources :users, has\_many =\> :owned\_clubs?

---

<div class="post-metadata">

**Author:** ![badnaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e36b37/32.png) [@badnaam](https://www.ruby-forum.com/u/badnaam)\
**Post date:** [May 15, 2010, 4:08am UTC](https://www.ruby-forum.com/t/tricky-model-situatione/188579/5 "2010-05-15T04:08:24Z")

</div>

Thanks Matt and Colin.
