I’m relatively new to Rails… first post here
Hope I’m not repeating an already asked question though I assume I’m
not the first one looking for how to best implement such an
association… Anyway, haven’t found the answers I was looking for when
searching for it, thus posting here a question. Apologise in advance
if it’s a repeated one… but would still appreciate any helpful
suggestion / direction / reference… a lot…
I started working on a project where there are hierarchical groups of
If I were to call the groups GroupAUsers, GroupBUsers, GroupCUsers,
GroupDUsers (may be more Groups, behaving the same way), then the
connection between the users in each group can be illustrated as
* GroupAUsers have many GroupBUsers, plus have their own
groupAUser attributes. They also have full access to all other User
groups hierarchically “below” GroupBUsers.
* GroupBUsers belong to GroupAUsers and have many GroupCUsers,
plus have their own specific GroupBUsers’ attributes. For each User of
GroupCUsers they have full access to his/her GroupDUsers.
* GroupCUsers belong to GroupBUsers and have many (each such user)
GroupDUsers, plus have their own specific attributes
* GroupDUsers belong to GroupCUsers and have their own specific
* And so on.. (in case of additional such groups)
A better description of this would probably be (writing code only to
explain, this is not a copy paste of my code, though it’s what I have
in mind right now as for associating the models)
class GroupAUser < ActiveRecord::Base
has_many :group_c_users, :through => :group_b_users
# Not sure how should the # has_many :group_d_users # association look like … # it's a 'nested through'.. # which means I need some help here as well in case that's what I
should be doing…
# plus it becomes worse in case I have additional groups
class GroupBUser < ActiveRecord::Base
has_many :group_d_users, :through => :group_c_users
class GroupCUser < ActiveRecord::Base
class GroupDUser < ActiveRecord::Base
If this is the way I illustrate the associations, I guess my User
model would look something like this:
class User < ActiveRecord::Base
so I have access from each User’s information to their suitable
information as being a user inside a group.
This is rather ugly, plus writing it down now, it seems wrong…
Reason I’m not using inheritance (say, from my User model) is I do not
wish to end up with one huge table, rails being STI… mainly that…
Another important point is a user can belong to several groups and
should be able to see all relevant information according to his/her
role in each group.
Not sure yet how to best implement this “feature” as well… if anyone
has any suggestions he/she cares to share, I’d love to learn
I’m currently using Rails 2.3.8 & Ruby 1.8.7.
Also, for authentication and authorization – planning on using
Authlogic and Declarative_Authorization.
If anyone can direct me to a “best practice” of modelling hierarchical
associations or suggest me anything else (including upgrading
versions, though I did not get the impression there are significant
changes from reading the associations Rails documentation), or even a
“you got it completely wrong” (with some explanation if you can please
be kind enough) – I’d really and truly appreciate it.
Hope I managed to be clear enough…