[Rails] team captain - habtm w/has_one...

Michael Barinek barinek at gmail.com
Wed Jul 26 12:50:17 GMT 2006


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?

-- 
Posted via http://www.ruby-forum.com/.


More information about the Rails mailing list