Problems with relations

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

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…

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