DataMapper - 为什么“有”和“属于”?
我刚刚开始使用 DataMapper,我试图弄清楚为什么需要指定 has
和 belongs_to
。
例如,查看 DataMapper 网站上的示例。这不是多余的吗?如果帖子有n
条评论,那么评论不会自动belongs_to
发布吗?为什么我必须指定这一点?
class Post
include DataMapper::Resource
property :id, Serial
has n, :comments
end
class Comment
include DataMapper::Resource
property :id, Serial
property :rating, Integer
belongs_to :post # defaults to :required => true
def self.popular
all(:rating.gt => 3)
end
end
I'm just getting started with DataMapper and I'm trying to figure out why you need to specify a has
and a belongs_to
.
For instance, look at the example on the DataMapper website. Isn't this redundant? If Post has n
comments, then doesn't Comment automatically belongs_to
post? Why do I have to specify this?
class Post
include DataMapper::Resource
property :id, Serial
has n, :comments
end
class Comment
include DataMapper::Resource
property :id, Serial
property :rating, Integer
belongs_to :post # defaults to :required => true
def self.popular
all(:rating.gt => 3)
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
仅当您想要使用额外规范生成的方法时,才指定关系的双方。它是完全可选的:如果您不需要从
Comment
访问Post
(例如@comment.post
),那么您就不需要在Comment
中指定belongs_to
关系。一个优点是您的实例更加干净,因为在
Comment
中附加方法不是自动生成的。另一方面,如果您需要它们,这些额外的方法不会打扰您。另请参阅有关 ActiveRecord 中关联的文档。
You specify both sides of the relationship only when you want to use the methods generated by the extra specification. It's completely optional: If you never need to get to the
Post
from theComment
(e.g.@comment.post
), you won't have to specify thebelongs_to
relation inComment
.One advantage is that your instances are a bit cleaner because in
Comment
the additional methods are not autogenerated. On the other hand, if you need them, those extra methods would not bother you.See also the documentation about associations in ActiveRecord.
这为您提供了轻松访问关系对象的方法。如
@post.comments
@comment.post
。我明白你的意思,应用 has_many 可能意味着属于_to。尽管考虑到开发人员添加belongs_to的开销,但可能比添加更多系统开销来动态地将方法添加到正确的类更好。另一件事是当通过另一个 has_many 关系使用 has_many 关系时。这会导致奇怪的 follow_to 关系,并且可能会导致 SQL 出现问题。
例如:
RolesUser 是一个连接表,它具有用户和角色模型的Belongs_to。在这种情况下暗示“属于”会将“belongs_to”添加到用户的角色模型中。这也没有意义,由于缺少数据库列,它也无法工作。当然,当存在 through 选项时,可以对此进行调整,但是当不需要时,这又会大大提高代码的复杂性。正如达安在他的回答中所说,你不需要两者都起作用,它是可选的。
That gives you the methods to access the relational object easily. Such as
@post.comments
@comment.post
. I see what you mean, applying has_many could imply the belongs_to. Though given the developer overhead to add the belongs_to, is probably better than adding more system overhead to dynamically add the methods to the right class.Another thing would be when using the has_many relation ship through another has_many relationship. That would cause odd belong_to relationships, and would probably cause issues with the SQL.
For example:
The RolesUser being a join table that has belongs_to for the both the user and the role models. Implying the belongs to in this case would then add a belongs_to to the role model to a user. Nor does this not make sense, it also wouldnt work due to the lack of the database column being there. Of course this could be adjusted when the through option is there, but again this would highly raise the complexity of the code when it's not needed. As Daan said in his answer, you do not need both for it to work, it's optional.
我想补充一下这些好的答案,如果您需要
dm-constraints
(直接或通过data_mapper
)并使用auto_migrate!
,那么belongs_to
将自动在数据库级别添加外键约束,而has
单独无法做到这一点。例如:
生成此内容(通过 MySQL 适配器):
如果您在
Post
中使用has n, :comments
但选择不包含belongs_to :post
Comment
,comments
表中的post_id
列仍将被创建并建立索引,但不会添加外键约束。I'd like to add to these good answers that if you have required
dm-constraints
(directly or viadata_mapper
) and useauto_migrate!
, thenbelongs_to
will automatically add foreign key constraints at the database level, whereashas
alone will not do this.e.g.:
Produces this (via MySQL adapter):
If you use
has n, :comments
withinPost
but choose not to includebelongs_to :post
withinComment
, thepost_id
column in thecomments
table will still be created and indexed, but no foreign key constraint will be added.