From: Anthony Eden Date: 2007-12-17T03:49:32+09:00 Subject: Re: teams -> members -> users 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 %> > > <%= render :partial => 'user' %> > > <% 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/. > >