DataMapper 子类化 &多对多的自引用关系

发布于 2024-10-07 05:38:20 字数 1057 浏览 3 评论 0原文

我正在使用 DataMapper 和 Sinatra 构建一个小型 Rub​​y 应用程序,并且尝试定义一个基本博客模型:

  • 博客有多个用户
  • 我有一个帖子集合,每个帖子都由一个用户发布
  • 每个帖子都有一组评论数
  • 每个评论都可以有自己的一组评论 - 这可能会重复几个深度级别

由于每个评论都属于一个帖子,因此我在获取评论之间的自引用关系时遇到了麻烦。我的课程现在看起来像这样:

class User
  include DataMapper::Resource
  property :id, Serial
  property :username, String
  property :password, String

  has n, :post
end
class Post
  include DataMapper::Resource
  property :id, Serial
  property :content, Text

  belongs_to :user

  has n, :comment
end
class Comment
  include DataMapper::Resource
  property :id, Serial
  property :content, Text

  belongs_to :user
  belongs_to :post
end

我正在遵循 Associations 中的指南并构建一个新对象 (CommentConnection )将两个评论链接在一起,但我的问题是每个子评论不应该属于 Comment 类所暗示的 Post。

我的第一直觉是提取评论的超类,以便一个子类可以是“顶级”并属于一个帖子,而另一种评论属于另一个评论。不幸的是,当我这样做时,我遇到了评论 ID 变为空的问题。

在 DataMapper 中建模这种递归注释关系的最佳方法是什么?

I'm building a small Ruby application using DataMapper and Sinatra, and I'm trying to define a basic blog model:

  • The blog has multiple Users
  • I have a collection of Posts, each of which is posted by a User
  • Each Post has a set of Comments
  • Each Comment can have its own set of Comments - this can repeat several levels deep

I'm running into trouble getting the self-referential relation between comments going due to the fact that each Comment belongs_to a Post. My classes right now look like this:

class User
  include DataMapper::Resource
  property :id, Serial
  property :username, String
  property :password, String

  has n, :post
end
class Post
  include DataMapper::Resource
  property :id, Serial
  property :content, Text

  belongs_to :user

  has n, :comment
end
class Comment
  include DataMapper::Resource
  property :id, Serial
  property :content, Text

  belongs_to :user
  belongs_to :post
end

I'm following the guide at Associations and building a new object (CommentConnection) to link two comments together, but my issue is that each subcomment shouldn't belong to a Post as implied by the Comment class.

My first instinct was to extract out a superclass for Comments, so that one subclass could be "top-level" and belong to a post, while the other kind of comment belongs to another comment. Unfortunately, when I do that I run into issues with the comment IDs becoming null.

What's the best way to model this kind of recursive comment relationship in DataMapper?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

他夏了夏天 2024-10-14 05:38:21

您需要的是评论中的自引用连接,例如,每个评论可以有一个父评论。尝试以下操作:

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

  has n, :replies, :child_key => [ :original_id ]
  belongs_to :original,  self, :required => false #Top level comments have none.
  belongs_to :user
  belongs_to :post
end

这将允许您回复任何给定的评论,尽管如果音量变高,访问它们可能会有点令人讨厌(缓慢)。如果你想让这个工作正常并且想要一些更复杂的东西,你可以看看嵌套集,我相信有一个 DataMapper 的嵌套集插件,但我没有使用过。

What you need is a self referential join in Comments, e.g., each Comment can have a parent comment. Try the following:

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

  has n, :replies, :child_key => [ :original_id ]
  belongs_to :original,  self, :required => false #Top level comments have none.
  belongs_to :user
  belongs_to :post
end

This will allow you to have replies to any given comment, although accessing them may get a little nasty (slow) if the volume gets high. If you get this working and want something more sophisticated you could look at nested sets, I believe there is a nested sets plugin for DataMapper but I haven't used.

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