teams -> members -> users

J

John Griffiths

trying to work this out, giving me a headache,

basically i've got a data structure where you have teams, members and
users.

each team can have many members,
each member has one user, and is assigned to one team,

----------
class Team < ActiveRecord::Base
has_many :members, :class_name => 'Member'
validates_presence_of :team_name
end

----------
class Member < ActiveRecord::Base
belongs_to :team
has_one :user, :class_name => 'User'
end

----------
class User < ActiveRecord::Base
belongs_to :member
end



so now in my team:index page, i want to show all the users how are
members of a team.

i've tried...

controller...
@team = Team.find(self.current_user.member.team_id)

view...

<% for @member in @team.members %>
<td width="50%">
<%= render :partial => 'user' %>
</td>
<% end %>

partial...
<% for @user in @member.users %>
<%= @user.email %>
<% end %>

...this in theory should work (and does up to the partial), once i get
to the partial, rails throws "undefined method 'users'"

any ideas where i've gone wrong?
 
A

Anthony Eden

In the future you should post Rails questions to the Rails mailing
list at http://groups.google.com/group/rubyonrails-talk

Having said that, this should probably be modeled as a "has many
through" relationship (note that code below is untested):

class User < ActiveRecord::Base
has_many :memberships
has_many :teams, :through => :memberships
end

class Team < ActiveRecord::Base
has_many :memberships
has_many :users, :through => :memberships
end

class Membership < ActiveRecord::Base
belongs_to :user
belongs_to :team
end

You would then be able to do some_team.users to get all of the users
for the specified team.

HTH
Anthony Eden
 
J

John Griffiths

thanks, sort of works.

isn't it possible to do:

has_one :member
has_one :team, :through => member

every time i try i get...

undefined key(s) through
 
T

Todd Benson

thanks, sort of works.

isn't it possible to do:

has_one :member
has_one :team, :through => member

every time i try i get...

undefined key(s) through

I don't know your model, but is there a reason why a member and a user
have to be different tables/classes? (I'm going by the descriptions
of your data relations)

For lack of more information, it appears to me there is something
wrong with your data model, like maybe you are trying to shoehorn what
you consider unstable identity (user) into stable identity (member).
I don't see that as a useful relational set up. What's your use
case?.

I think most database guys would agree that one to one relations are
simple tuples. Why would you be required to make them different
tables/classes? From your original post, a member _is_ a user. The
only thing I can think of why you would want to separate them is
because that relation changes over time, which can easily be handled
by the db (think "create table user_update...").

Todd
 
J

John Griffiths

nehah!!!

we have it working.....


user
---
has_one :member, :class_name => 'Member'

member
------
belongs_to :team, :class_name => 'Team'
belongs_to :user, :class_name => 'User'

team
----
has_many :members, :class_name => 'Member'
has_many :users, :through => :members

--------------------------------

and access with...

<%= self.current_user.member.team.team_name.capitalize %>

&

<% for @user in @team.users %>
<td width="50%">
<%= @user.profile.firstname %> <%= @user.profile.lastname %>
</td>
<% end %>

works like a charm!

thank you so much Anthony!

Merry Christmas!!!
 
T

Todd Benson

I don't know your model, but is there a reason why a member and a user
have to be different tables/classes? (I'm going by the descriptions
of your data relations)

For lack of more information, it appears to me there is something
wrong with your data model, like maybe you are trying to shoehorn what
you consider unstable identity (user) into stable identity (member).
I don't see that as a useful relational set up. What's your use
case?.

I think most database guys would agree that one to one relations are
simple tuples. Why would you be required to make them different
tables/classes? From your original post, a member _is_ a user. The
only thing I can think of why you would want to separate them is
because that relation changes over time, which can easily be handled
by the db (think "create table user_update...").

Todd

Well, I certainly must apologize for that rant. I think I misread
what you said. It's just that I've seen a lot of models similar to
yours that were a nightmare to work with when doing reports; that's
all.

<bows out and tries not to get killed on the way out>

Todd
 
T

Todd Benson

thanks, sort of works.

isn't it possible to do:

has_one :member
has_one :team, :through => member

every time i try i get...

undefined key(s) through

Well, if your model is what I think it is, a team doesn't have one
member. A user doesn't have one member either. Both a team and a
user have zero or more "memberships". You can restrict the
membershipness to exactly one for the user in the database if you want
to with check constraints. (or you could just have 2 tables, team and
member). How is this multiple<->multiple handled in Rails? I think
it's handled like this (using ActiveRecord with the necessary table
names and primary key names, of course):

class Person < ActiveRecord::Base
has_many :members
has_many :teams, :through => :members
end

class Member < ActiveRecord::Base
has_one :person
has_one :team
end

class Team < ActiveRecord::Base
has_many :members
has_many :persons, :through => :members
end


not tested,

Todd
 
J

John Griffiths

ordinarily you'd be spot on, but in this case each user can only be
associated to one team and that team can have only two or less members
(users).

so glad i cracked this with all your help, major weight off my
shoulders, thanks guys!
 
M

Matt Todd

ordinarily you'd be spot on, but in this case each user can only be
associated to one team and that team can have only two or less members
(users).

I think with the unusual model you have (most users often are part of
many teams) you may be having a harder time just figuring out how the
relationships are in actuality simply for the fact that our minds tell
us something doesn't feel right because we know users often want to be
able to be on multiple teams.

We get mental friction when we know we're doing things wrong or a way
our mind doesn't exactly recognize as normal.

Not saying that you are, just say that it's possible that there could
be a better way to define the relationships. :)

With that said, if users really only belong to one team, then why
bother with memberships in the first place? You could easily have just
two tables and models if a user ever only has one team. (Though, like
I said, that's not usually the case).

M Todd
 
J

John Griffiths

Totally, I could have done it simpler.

But this way it just looks the nicer model, and if they ever decide to
go the route of users in more than one team it's already there.

Glad it's done, that was one big headache.

Thanks so much guys!
 
T

Todd Benson

Totally, I could have done it simpler.

But this way it just looks the nicer model, and if they ever decide to
go the route of users in more than one team it's already there.

Just be careful. Deciding to go the route of users in more than one
team is "there" will require rewriting crucial ruby code.
Glad it's done, that was one big headache.

Thanks so much guys!

Todd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top