# Problems with relations

**URL:** <https://www.ruby-forum.com/t/problems-with-relations/91458>\
**Category:** Rails\
**Created:** [April 9, 2007, 2:30pm UTC](https://www.ruby-forum.com/t/problems-with-relations/91458 "2007-04-09T14:30:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![azazel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/838e76/32.png) [@azazel](https://www.ruby-forum.com/u/azazel)\
**Post date:** [April 9, 2007, 2:30pm UTC](https://www.ruby-forum.com/t/problems-with-relations/91458/1 "2007-04-09T14:30:03Z")

</div>

Hello there,

Im working on an application which has the following Models:

User(id, username, password, email)  
Group(id, name, owner\_id)

I also have a table: groups\_users(group\_id, user\_id) to handle the  
many-to-many rel.

The relationship between Users and Groups is two-fold. A user can create  
many groups (those belong to him through owner\_id). He can then add  
other users to the group (many to many rel).

The problem is that if user ‘Apu’ creates a group and adds ‘Jon’ and  
‘Bob’, as far as ‘Jon’ & ‘Bob’ (objects) are concerned, they dont belong  
to the group. (Jon.groups should return the list of groups created by  
‘Jon’, not the list of groups to which ‘Jon’ belongs to)

My models are the following:

class User \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :groups  
has\_many :owned\_groups, :class\_name =\> ‘Group’, :foreign\_key =\>  
:group\_id

validates\_uniqueness\_of :username, :email  
validates\_presence\_of :username, :passwd, :seed, :email  
validates\_length\_of :username, :passwd, :minimum =\> 5  
end

class Group \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :users  
belongs\_to :owner, :class\_name =\> ‘User’, :foreign\_key =\> :owner\_id

validates\_presence\_of :name  
end

I tried changing the has\_and\_belongs\_to’s to reflect the logic more  
closely, but then it wouldn’t use the groups\_users table and would throw  
errors because it didn’t find the fields in the model.

Using the current code, I manage to add the users to the group (it uses  
the groups\_users table) but when I go back to …/groups/list/ it  
displays the following error.

Showing app/views/groups/list.rhtml where line #10 raised:

You have a nil object when you didn’t expect it!  
You might have expected an instance of Array.  
The error occurred while evaluating nil.each

Extracted source (around line #10):

7: \<% end %\>  
8:   
9:  
10: \<% for group in @groups %\>  
11:

  
12: \<% for column in Group.content\_columns %\>  
13: \<%=h group.send(column.name) %\>

I’m really lost here. I recon my relationships in the models are  
probably wrong. I just can’t pinpoint a solution. I’ve been reading  
Agile Web D. with Rails but I havent found anything similar in  
the book.

Regards,  
az

---

<div class="post-metadata">

**Author:** ![azazel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/838e76/32.png) [@azazel](https://www.ruby-forum.com/u/azazel)\
**Post date:** [April 9, 2007, 3:51pm UTC](https://www.ruby-forum.com/t/problems-with-relations/91458/2 "2007-04-09T15:51:43Z")

</div>

Um… let’see:

Roberto R. wrote:

> The problem is that if user ‘Apu’ creates a group and adds ‘Jon’ and  
> ‘Bob’, as far as ‘Jon’ & ‘Bob’ (objects) are concerned, they dont belong  
> to the group. (Jon.groups should return the list of groups created by  
> ‘Jon’, not the list of groups to which ‘Jon’ belongs to)

Not really; Jon.groups returns the groups to which Jon belongs.  
Jon.owned\_groups should return the list of groups created by Jon.

I think your models are correct, but you are using them wrongly.  
However, the error in your groups/list view seems nothing to do with  
models relations, just with the Group model…

---

<div class="post-metadata">

**Author:** ![azazel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/838e76/32.png) [@azazel](https://www.ruby-forum.com/u/azazel)\
**Post date:** [April 9, 2007, 4:28pm UTC](https://www.ruby-forum.com/t/problems-with-relations/91458/3 "2007-04-09T16:28:50Z")

</div>

Thanks for the input.

Managed to get it working (or so I think). Basically the problem was the  
:owned\_groups foreign key. changed it to ‘owner\_id’ and it seems to work  
properly now. I was also able to remove the ‘has\_and\_belongs\_to\_many’  
rel.

class User \< ActiveRecord::Base  
has\_many :documents  
#has\_and\_belongs\_to\_many :groups  
has\_many :groups, :class\_name =\> ‘Group’, :foreign\_key =\> :owner\_id

validates\_uniqueness\_of :username, :email  
validates\_presence\_of :username, :passwd, :seed, :email  
validates\_length\_of :username, :passwd, :minimum =\> 5  
end
