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: </tr>
9:
10: <% for group in @groups %>
11: <tr>
12: <% for column in Group.content_columns %>
13: <td><%=h group.send(column.name) %></td>
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 Development with Rails but I havent found anything similar in
the book.
Regards,
az
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---