30

I need to do two associations in the same model. Where:

Team has_many User Now, I want that Team has_one Leader

This "Leader" will be a User

Im trying to use has_one throught but I think that association isn't work.

Leader.rb

class Leader < ActiveRecord::Base
belongs_to :user
belongs_to :team

Team.rb

class Team < ActiveRecord::Base
has_one :user, through: :leader
end

User.rb

class User < ActiveRecord::Base

belongs_to :team
has_one :captain

end

and the get following error around line 27:

NoMethodError in TeamsController#create

26 def create

**27 @team = current_user.teams.create(team_params)**

28 @team.save

29 respond_with(@team)

30 current_user.update(team_id: @team.id)
RAJ
  • 9,697
  • 1
  • 33
  • 63
Igor Martins
  • 2,015
  • 7
  • 36
  • 57
  • Can a user be in more teams, or only one team? – nathanvda Apr 14 '15 at 09:21
  • because `current_user` is a user, not a leader/captain, for your method to work you need `current_user.(leader/captain).teams.create(team_params)`, also check your assocciations, is it leader or is it captain – Mohammad AbuShady Apr 16 '15 at 06:31

5 Answers5

26

In this case I think you need 2 model are enough

1). User model

class User < ActiveRecord::Base
   belongs_to :team
end

2). Team model

 class Team < ActiveRecord::Base
   has_many :users
   belongs_to :leader, class_name: 'User', foreign_key: :leader_id
 end
Luan D
  • 1,320
  • 1
  • 13
  • 26
5

How about setting a boolean flag in users table called leader. And then your association can become:

class Team < ActiveRecord::Base   
  has_many :users   
  has_one :leader, class_name: 'User', -> { where leader: true }
end
usmanali
  • 2,028
  • 2
  • 27
  • 38
4

Team has_many User Now, I want that Team has_one Leader

This "Leader" will be a User

Use inheritance (also called sub-classing), Leader is a User.

class User < ActiveRecord::Base
    belongs_to :team
end

class Leader < User
end

class Team < ActiveRecord::Base
    has_many :users
    has_one :leader
end

Your users table is also important. Ensure that users has t.belongs_to :team and t.string :type in its create_table method. Note that a Leader is a User and does not need a separate table, however you do need to allow ActiveRecord to record its type so it can return the correct Model later.

References:

inheritance specifically you need 'single table inheritance'

belongs_to scroll down for has_one and has_many, the three relationships used here.

Matt Stevens
  • 1,104
  • 1
  • 12
  • 24
  • I think this is better as it allows more changes in the future with only one additional column (type). With the accepted answer, the `leader_id` column only works for leaders... – Pak Nov 04 '16 at 15:53
1

current_user.teams.create(team_params)

Teams is for a has_many association, you want current_user.create_team(team_params)

j-dexx
  • 10,286
  • 3
  • 23
  • 36
1

You have has_one association between user and team. Try this:

current_user.create_team(team_params)

Also, you should add proper back association from team to leader.

class Team < ActiveRecord::Base
  belongs_to :leader
  has_one :user, through: :leader
end
RAJ
  • 9,697
  • 1
  • 33
  • 63