0

我的数据类似于:

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

class Person < ActiveRecord::Base
  belongs_to :team
end

一个人只属于一个团队,但在众多团队成员中只有一个领导者。

第一个问题:我应该在 Team 模型中使用 belongs_to 而不是 has_one 吗?

第二:团队是由许多人创建的,并且最初知道领导者。这应该怎么做?

目前我在我的控制器中做这样的事情:

  @team = Team.new

  for (each new person as p)

    new_person = @team.persons.build
    new_person.name = p.name

    if p.is_marked_as_leader
      @team.leader = new_person
    end
  end

  @team.save

这是一个问题,当我列出 @team.persons 时,@team.leader 有第一个 id,我假设是因为 @team.save 首先保存了领导者关联。我需要它们按照提供的顺序排列。

谢谢!

4

1 回答 1

0

I would vote for 'has_one' for the leader, because you the person can exist outside the team and her role as team lead.

It is the aggregation vs composition discussion.

Sometimes this is open to debate, but in this case I would say the team - leader relationship is clearly composition.

于 2010-07-20T16:46:40.987 回答