轨道3,ActiveRecord的,任何"陷阱"如果使用的has_many没有相应belongs_to的? [英] rails 3, activerecord, any "gotchas" if use has_many WITHOUT corresponding belongs_to?

查看:103
本文介绍了轨道3,ActiveRecord的,任何"陷阱"如果使用的has_many没有相应belongs_to的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个电话的has_many消息

A phone has_many messages

这是电子邮件地址的has_many消息

An email address has_many messages

消息,有时属于一个电话,有时一封电子邮件,有时没有。换言之,所述belongs_to的是可选的。

a message "sometimes belongs to" a phone, sometimes an email, sometimes neither. In other words, the "belongs_to" is optional.

这似乎做工精细,我们的手机型号的has_many:消息和我们的电子邮件模式的has_many:消息没有相应的belongs_to的:电话,:邮件。邮件中的模型

It SEEMS to work fine that our phones model "has_many :messages" and our email model "has_many :messages" WITHOUT a corresponding "belongs_to :phones, :email" in messages model.

只是想确保没关系做到这一点?或者是有一些适当的方法来指定belongs_to_SOMETIMES?

Just wanted to ensure that's okay to do this? or is there some proper way to specify belongs_to_SOMETIMES ?

干杯! JP

推荐答案

这是完全正确的单向关系。同时使用有时被称为curcular依赖性由一些较真和可能导致问题时使用<一href="http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#method-i-validates_associated"相对=nofollow> validates_associated 。

It is completely correct unidirectional relation. Using both is sometimes called "curcular dependency" by some purists and may cause problems when using validates_associated.

这是只使用的has_many的另一面:消息可能是不够的,当你想从一个信息检索的电话信​​息。通常它是方便的事。

From the other side using only has_many :messages may be not enough when you want retrieve phone information from one message. Generally it is matter of convenience.

这篇关于轨道3,ActiveRecord的,任何&QUOT;陷阱&QUOT;如果使用的has_many没有相应belongs_to的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆