teams -> members -> users

Discussion in 'Ruby' started by John Griffiths, Dec 16, 2007.

  1. 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?
    --
    Posted via http://www.ruby-forum.com/.
     
    John Griffiths, Dec 16, 2007
    #1
    1. Advertising

  2. John Griffiths

    Anthony Eden Guest

    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

    On Dec 16, 2007 1:27 PM, John Griffiths <> wrote:
    > 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?
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
     
    Anthony Eden, Dec 16, 2007
    #2
    1. Advertising

  3. 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

    --
    Posted via http://www.ruby-forum.com/.
     
    John Griffiths, Dec 16, 2007
    #3
  4. John Griffiths

    Todd Benson Guest

    On Dec 16, 2007 1:45 PM, John Griffiths <> wrote:
    > 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
     
    Todd Benson, Dec 16, 2007
    #4
  5. 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!!!
    --
    Posted via http://www.ruby-forum.com/.
     
    John Griffiths, Dec 16, 2007
    #5
  6. John Griffiths

    Todd Benson Guest

    On Dec 16, 2007 3:48 PM, Todd Benson <> wrote:
    > On Dec 16, 2007 1:45 PM, John Griffiths <> wrote:
    > > 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


    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
     
    Todd Benson, Dec 16, 2007
    #6
  7. John Griffiths

    Todd Benson Guest

    On Dec 16, 2007 1:45 PM, John Griffiths <> wrote:
    > 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
     
    Todd Benson, Dec 16, 2007
    #7
  8. 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!
    --
    Posted via http://www.ruby-forum.com/.
     
    John Griffiths, Dec 16, 2007
    #8
  9. John Griffiths

    Matt Todd Guest

    > 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
     
    Matt Todd, Dec 17, 2007
    #9
  10. 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!
    --
    Posted via http://www.ruby-forum.com/.
     
    John Griffiths, Dec 17, 2007
    #10
  11. John Griffiths

    Todd Benson Guest

    On Dec 17, 2007 9:33 AM, John Griffiths <> wrote:
    > 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
     
    Todd Benson, Dec 17, 2007
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. zcraven
    Replies:
    3
    Views:
    2,614
    Bruno Grieder
    Oct 29, 2004
  2. Replies:
    0
    Views:
    338
  3. Diwa
    Replies:
    24
    Views:
    766
  4. Jeff

    creating random teams?

    Jeff, Dec 29, 2006, in forum: ASP General
    Replies:
    9
    Views:
    182
  5. Daniel Moore

    [QUIZ] IRC Teams (#221)

    Daniel Moore, Oct 16, 2009, in forum: Ruby
    Replies:
    1
    Views:
    153
    Martin DeMello
    Oct 19, 2009
Loading...

Share This Page