Rails 3:如何为有父亲,母亲和最多两名监护人的孩子设置入学协会?

时间:2012-07-11 19:16:28

标签: ruby-on-rails ruby associations

如何为有父亲,母亲和最多两名监护人的儿童设立入学协会?

我知道有很多方法可以解决这个问题,但你会怎么做呢?我不太确定如何设置监护人。我目前正在考虑两种选择,也许你可以推荐第三种选择。

选项1

Enrollments
belongs_to child
belongs_to father
belongs_to mother

Children
has_one enrollment
has_one father
has_one mother

Father
has_many enrollments
has_many children

Mother
has_many enrollments
has_many children

选项2

Enrollments
has_and_belongs_to_many adults

Adults
has_and_belongs_to_many enrollments
has_many children
# would have a "relationship: father, mother, guardian"

Children
has_one enrollment
has_one father
has_one mother

1 个答案:

答案 0 :(得分:2)

如果我是你,我会查看has_many:

成年人

has_many children, :through => enrollments

儿童

has many adults, :through => enrollments