DataMapper - 为什么“有”和“属于”?

发布于 2024-12-03 08:22:52 字数 526 浏览 1 评论 0原文

我刚刚开始使用 DataMapper,我试图弄清楚为什么需要指定 hasbelongs_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苏大泽ㄣ 2024-12-10 08:22:52

仅当您想要使用额外规范生成的方法时,才指定关系的双方。它是完全可选的:如果您不需要从 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 the Comment (e.g. @comment.post), you won't have to specify the belongs_to relation in Comment.

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.

弱骨蛰伏 2024-12-10 08:22:52

这为您提供了轻松访问关系对象的方法。如@post.comments@comment.post。我明白你的意思,应用 has_many 可能意味着属于_to。尽管考虑到开发人员添加belongs_to的开销,但可能比添加更多系统开销来动态地将方法添加到正确的类更好。

另一件事是当通过另一个 has_many 关系使用 has_many 关系时。这会导致奇怪的 follow_to 关系,并且可能会导致 SQL 出现问题。

例如:

class User < ActiveRecord::Base
  has_many :roles, :through => :roles_users
  has_many :roles_users
end

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:

class User < ActiveRecord::Base
  has_many :roles, :through => :roles_users
  has_many :roles_users
end

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.

剩一世无双 2024-12-10 08:22:52

我想补充一下这些好的答案,如果您需要dm-constraints(直接或通过data_mapper)并使用auto_migrate!,那么 belongs_to 将自动在数据库级别添加外键约束,而 has 单独无法做到这一点。

例如:

require 'data_mapper'

class Post
  include DataMapper::Resource
  property :id, Serial
end

class Comment
  include DataMapper::Resource
  property :id, Serial

  belongs_to :post
end

生成此内容(通过 MySQL 适配器):

 ~ (0.140343) CREATE TABLE comments (id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
post_id INT(10) UNSIGNED NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB
CHARACTER SET utf8 COLLATE utf8_general_ci

 ~ (0.234774) CREATE INDEX index_comments_post ON comments (post_id)

 ~ (0.433988) ALTER TABLE comments ADD CONSTRAINT comments_post_fk
FOREIGN KEY (post_id) REFERENCES posts (id) ON DELETE NO ACTION ON UPDATE NO ACTION

如果您在 Post 中使用 has n, :comments 但选择不包含 belongs_to :post Commentcomments 表中的 post_id 列仍将被创建并建立索引,但不会添加外键约束。

I'd like to add to these good answers that if you have required dm-constraints (directly or via data_mapper) and use auto_migrate!, then belongs_to will automatically add foreign key constraints at the database level, whereas has alone will not do this.

e.g.:

require 'data_mapper'

class Post
  include DataMapper::Resource
  property :id, Serial
end

class Comment
  include DataMapper::Resource
  property :id, Serial

  belongs_to :post
end

Produces this (via MySQL adapter):

 ~ (0.140343) CREATE TABLE comments (id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
post_id INT(10) UNSIGNED NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB
CHARACTER SET utf8 COLLATE utf8_general_ci

 ~ (0.234774) CREATE INDEX index_comments_post ON comments (post_id)

 ~ (0.433988) ALTER TABLE comments ADD CONSTRAINT comments_post_fk
FOREIGN KEY (post_id) REFERENCES posts (id) ON DELETE NO ACTION ON UPDATE NO ACTION

If you use has n, :comments within Post but choose not to include belongs_to :post within Comment, the post_id column in the comments table will still be created and indexed, but no foreign key constraint will be added.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文