# Team captain - habtm w/has\_one

**URL:** <https://www.ruby-forum.com/t/team-captain-habtm-w-has-one/99980>\
**Category:** Rails\
**Created:** [July 26, 2006, 2:50pm UTC](https://www.ruby-forum.com/t/team-captain-habtm-w-has-one/99980 "2006-07-26T14:50:17Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![michaelb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b19c9b/32.png) [@michaelb](https://www.ruby-forum.com/u/michaelb)\
**Post date:** [July 26, 2006, 2:50pm UTC](https://www.ruby-forum.com/t/team-captain-habtm-w-has-one/99980/1 "2006-07-26T14:50:17Z")

</div>

the below…

class User \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :teams

class Team \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :users  
has\_one :captain, :class\_name =\> ‘User’

produces the error…

Mysql::Error: Unknown column ‘users.team\_id’ in ‘where clause’: SELECT \*  
FROM users WHERE (users.team\_id = 1) LIMIT 1

i realize the error - and my current solution is to add user\_id to  
team…

then i have…

class Team \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :users

def captain  
User.find(user\_id)  
end

although the above solution seems awkward - is there a better way to  
accomplish the above?

---

<div class="post-metadata">

**Author:** ![michaelb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b19c9b/32.png) [@michaelb](https://www.ruby-forum.com/u/michaelb)\
**Post date:** [July 26, 2006, 3:06pm UTC](https://www.ruby-forum.com/t/team-captain-habtm-w-has-one/99980/2 "2006-07-26T15:06:34Z")

</div>

class User \< ActiveRecord::Base  
has\_many :memberships  
has\_many :teams, :through =\> :memberships

class Team \< ActiveRecord::Base  
has\_many :memberships  
has\_many :users, :through =\> :memberships  
has\_one :captain, :class\_name =\> ‘User’, :conditions =\>  
[‘memberships.captain = ?’, true], :through =\> :memberships  
end

class Membership \< ActiveRecord::Base  
end

I haven’t run this, but it looks nice.

---

<div class="post-metadata">

**Author:** ![michaelb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b19c9b/32.png) [@michaelb](https://www.ruby-forum.com/u/michaelb)\
**Post date:** [July 26, 2006, 3:07pm UTC](https://www.ruby-forum.com/t/team-captain-habtm-w-has-one/99980/3 "2006-07-26T15:07:54Z")

</div>

Your memberships table would need a boolean ‘captain’ field, which is  
set to true for the user,club combination as appropriate

---

<div class="post-metadata">

**Author:** ![michaelb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b19c9b/32.png) [@michaelb](https://www.ruby-forum.com/u/michaelb)\
**Post date:** [July 26, 2006, 3:24pm UTC](https://www.ruby-forum.com/t/team-captain-habtm-w-has-one/99980/4 "2006-07-26T15:24:20Z")

</div>

Dr Nic wrote:

> Your memberships table would need a boolean ‘captain’ field, which is  
> set to true for the user,club combination as appropriate

is memberships similar to teams\_users?

---

<div class="post-metadata">

**Author:** ![michaelb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b19c9b/32.png) [@michaelb](https://www.ruby-forum.com/u/michaelb)\
**Post date:** [July 26, 2006, 6:24pm UTC](https://www.ruby-forum.com/t/team-captain-habtm-w-has-one/99980/5 "2006-07-26T18:24:39Z")

</div>

last question…

so i still have a problem with select boxes and the controller…

class Team \< ActiveRecord::Base  
has\_many :memberships  
has\_many :users, :through =\> :memberships

class User \< ActiveRecord::Base  
has\_many :memberships  
has\_many :teams, :through =\> :memberships

class Membership \< ActiveRecord::Base  
belongs\_to :user  
belongs\_to :team

def create  
@team = Team.new(params[:team])

NoMethodError in TeamController#create

undefined method `user\_ids=’ for #Team:0x2789500

so i looked - user\_ids isn’t there - just membership\_ids…

---

<div class="post-metadata">

**Author:** ![michaelb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b19c9b/32.png) [@michaelb](https://www.ruby-forum.com/u/michaelb)\
**Post date:** [July 26, 2006, 3:28pm UTC](https://www.ruby-forum.com/t/team-captain-habtm-w-has-one/99980/6 "2006-07-26T15:28:03Z")

</div>

Michael B. wrote:

> Dr Nic wrote:
> 
> > Your memberships table would need a boolean ‘captain’ field, which is  
> > set to true for the user,club combination as appropriate
> 
> is memberships similar to teams\_users?

Yep. Same columns (plus the captain column), with a better name.

---

<div class="post-metadata">

**Author:** ![michaelb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b19c9b/32.png) [@michaelb](https://www.ruby-forum.com/u/michaelb)\
**Post date:** [July 26, 2006, 7:32pm UTC](https://www.ruby-forum.com/t/team-captain-habtm-w-has-one/99980/7 "2006-07-26T19:32:54Z")

</div>

i solved the above using the below…

def create  
@team = Team.new(params[:team])  
if @team.save  
flash[:notice] = ‘Team was successfully created.’

```
Membership.new(:user => @session['user'], :team => @team, 

```

:is\_the\_captain =\> true).save

```
User.find(params[:membership][:user_ids]).each do |user|
  Membership.new(:user => user, :team => @team).save
end

```

still seems strange…

thoughts/comments?

---

<div class="post-metadata">

**Author:** ![michaelb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b19c9b/32.png) [@michaelb](https://www.ruby-forum.com/u/michaelb)\
**Post date:** [July 26, 2006, 11:47pm UTC](https://www.ruby-forum.com/t/team-captain-habtm-w-has-one/99980/8 "2006-07-26T23:47:49Z")

</div>

```
User.find(params[:membership][:user_ids]).each do |user|
  Membership.new(:user => user, :team => @team).save
end

```

I don’t get why you’re doing this. If you’re creating a new Team, then  
the creator will be the first and only member at that time.

---

<div class="post-metadata">

**Author:** ![michaelb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b19c9b/32.png) [@michaelb](https://www.ruby-forum.com/u/michaelb)\
**Post date:** [July 27, 2006, 3:05am UTC](https://www.ruby-forum.com/t/team-captain-habtm-w-has-one/99980/9 "2006-07-27T03:05:42Z")

</div>

what if they don’t select their-self?

here is the view…

\<% @selected = @team.users.collect { |v| v.id.to\_i } %\>  
  
\<%= options\_from\_collection\_for\_select(@users, “id”, “login”,  
@selected) %\>

and the db…

create table memberships (  
id int(10) unsigned not null auto\_increment,  
team\_id int(10) unsigned not null,  
user\_id int(10) unsigned not null,  
is\_the\_captain tinyint(1) not null default ‘0’,  
primary key (id),  
unique (team\_id, user\_id),  
foreign key (team\_id) references teams(id),  
foreign key (user\_id) references users(id)  
) engine=innodb default charset=latin1;
