Forum: Ruby on Rails has_many questions

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Michael J. (Guest)
on 2006-04-03 04:26
(Received via mailing list)
My application has two models I'd like to have multiple join tables
between, Users and Groups.  Users have the ability to subscribe to a
Group, while a Group would like to know its membership.
Additionally, a User can administer several Groups and a Group should
know about its several Administators (Users).

I have the following declarations:

class Administrator < ActiveRecord::Base
   belongs_to :user
   belongs_to :group

class GroupSubscription < ActiveRecord::Base
   belongs_to :user
   belongs_to :group

class Group < ActiveRecord::Base
   has_many :group_subscriptions
   has_many :users, :through => :group_subscriptions
   has_many :administrators
   has_many :users, :through => :administrators

class User < ActiveRecord::Base
     has_many :group_subscriptions
     has_many :groups, :through => :group_subscriptions
     has_many :administrators
     has_many :groups, :through => :administrators

In the database, I have Users, Groups, Group_Subscriptions, and
Administrators (admin holds a user_id, and group_id) tables.
Obviously, what seems to be happening is that the second :groups
declaration overrides the first, preventing me from finding the
groups that someone is subscribed to.  It seems as if I should want
to provide a different name for the association while still
specifying that it should be between Users and Groups.  In essence,
saying the following in User:

	has_many :administrators
	has_many: groups_administered, :through => :administrators

However, this causes Active Record (I think) to no longer know what
it should be joining to with this particular association.  Can anyone
provide some tips on how to overcome this?

Michael J.
This topic is locked and can not be replied to.