has_many :通过多个 has_one 关系?
我正在用 Rails 为我们的教堂编写一个指导计划(我对 Rails 还很陌生)。
我需要对此进行建模。
contact
has_one :father, :class_name => "Contact"
has_one :mother, :class_name => "Contact"
has_many :children, :class_name => "Contact"
has_many :siblings, :through <Mother and Father>, :source => :children
所以基本上一个对象“兄弟姐妹”需要映射来自父亲和母亲的所有孩子,而不是包括物体本身..
这可能吗?
谢谢
丹尼尔
I'm writing a mentorship program for our church in rails (im still farily new to rails)..
And i need to model this..
contact
has_one :father, :class_name => "Contact"
has_one :mother, :class_name => "Contact"
has_many :children, :class_name => "Contact"
has_many :siblings, :through <Mother and Father>, :source => :children
So basically an objects "siblings" needs to map all the children from both the father and mother not including the object itself..
Is this possible?
Thanks
Daniel
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有趣的是,看似简单的问题却有复杂的答案。在这种情况下,实现反射性父/子关系相当简单,但添加父/母和兄弟姐妹关系会产生一些麻烦。
首先,我们创建表来保存父子关系。关系有两个外键,都指向联系人:
在关系模型中,我们将父亲和母亲指向联系人:
并在联系人中定义逆关联:
现在可以创建关系:
这不是很好,这是我们真正想要的是在一次调用中建立关系:
所以我们可以这样做:
要查找联系人的子级,请向联系人添加范围和(为了方便起见)实例方法:
兄弟姐妹是棘手的部分。我们可以利用 Contact.children 方法并操纵结果:
这不是最佳的,因为father.children 和 mother.children 会重叠(因此需要
uniq
),并且可以更有效地完成通过制定必要的 SQL(作为练习:)),但请记住self.father.children
和self.mother.children
不会重叠在同父异母兄弟姐妹(同父异母)的情况下,联系人可能没有父亲或母亲。以下是完整的型号和一些规格:
It's funny how questions that appear simple can have complex answers. In this case, implementing the reflexive parent/child relationship is fairly simple, but adding the father/mother and siblings relationships creates a few twists.
To start, we create tables to hold the parent-child relationships. Relationship has two foreign keys, both pointing at Contact:
In the Relationship model we point the father and mother back to Contact:
and define the inverse associations in Contact:
Now a relationship can be created:
This is not so great, what we really want is to build the relationship in a single call:
so we can do:
To find the children of a Contact, add a scope to Contact and (for convenience) an instance method:
Siblings are the tricky part. We can leverage the Contact.children method and manipulate the results:
This is non-optimal, since father.children and mother.children will overlap (thus the need for
uniq
), and could be done more efficiently by working out the necessary SQL (left as an exercise :)), but keeping in mind thatself.father.children
andself.mother.children
won't overlap in the case of half-siblings (same father, different mother), and a Contact might not have a father or a mother.Here are the complete models and some specs:
我完全同意泽泰蒂克的观点。这个问题看起来比答案简单得多,我们对此无能为力。不过我会加上 20c。
表:
表关系没有对应的模型。
现在我们有定期的 Rails 协会。所以我们可以
做所有类似的事情。
I totally agree with zetetic. The question looks far more simpler then the answer and there is little we could do about it. I'll add my 20c though.
Tables:
Table relations does not have corresponding model.
Now we have regular Rails assosiations. So we can
and all stuff like that.
我使用了这个示例,但必须添加 :delete_sql 来清理关系记录。起初我在字符串周围使用双引号,但发现这会导致错误。切换到单引号有效。
I used this example but had to add the :delete_sql to clean up the relations records. At first I used double quotes around the string but found that caused errors. Switching to single quotes worked.