Ruby Datamapper 表继承与关联

发布于 2024-08-12 22:45:00 字数 1477 浏览 4 评论 0原文

我开始学习 Datamapper,我喜欢它的是我可以用真正的继承来编写我的模型。

现在我想知道,是否可以对此进行更高级的处理:

class Event
  include DataMapper::Resource
  property :id, Serial
  property :begin, DateTime
  property :type, Discriminator
end

class Talk<Event
  property :title, String
  belongs_to :meeting
end

class Meeting<Event
  has n, :talks
end

该代码无法为 Talk 创建 :title 列,显然,鉴别器列几乎没有什么作用这里的值,因为从数据库视图来看,TalkMeeting 应该有单独的表。

因此,最后,我希望 TalkMeeting 共享 Event 中定义的相同属性,但具有可能的附加属性和 0 ..1:n 关系(一次会议可以进行多次会谈,但也有没有会议的会谈。)有没有办法在不重复列定义和/或放弃继承的情况下完成此任务?

编辑

再举一个例子:我喜欢继承的部分是,可以单独查询一般的Event。因此,当我想知道在某个 :begin 日期是否有某些内容时,我不需要查看两个或多个表,而只需查询 Event 表。在某种程度上,以下结构可以满足我的需求。

class Event
  include DataMapper::Resource
  property :id, Serial
  property :begin, DateTime
end

class Talk
  include DataMapper::Resource
  property :id, Serial
  property :title, String
  belongs_to :event
  belongs_to :meeting
end

class Meeting
  include DataMapper::Resource
  property :id, Serial
  belongs_to :event
  has n, :talks
end

但是,为了使用此功能,每次我想要创建或编辑 Talk 时,我都需要手动创建一个 Event。也就是说,我无法执行 talk.beginTalk.create(:begin => Time.now)。有没有办法解决这个问题而不修补所有功能并合并属性?我不想在使用模型时被提醒底层结构。

I started learning Datamapper and what I liked about it was that I can write my models with real inheritance.

Now I wonder, if it is possible to be more advanced about this:

class Event
  include DataMapper::Resource
  property :id, Serial
  property :begin, DateTime
  property :type, Discriminator
end

class Talk<Event
  property :title, String
  belongs_to :meeting
end

class Meeting<Event
  has n, :talks
end

That code fails to create the :title column for the Talk and obviously, the discriminator column is of little value here, because from a database view, there should be separate tables for both Talk and Meeting.

So, in the end, I want Talk and Meeting to share the same properties as defined in Event but with possible additional properties and with a 0..1:n relation (A meeting can have several talks but there are talks without a meeting.) Is there a way to accomplish this without either repeating the column definitions and/or abandoning inheritance?

Edit

To give another example: The part that I like about the inheritance thing is, that general Events can be queried separately. So, when I want to know, if there is something at a certain :begin date, I don’t need to look in two or more tables but could just query the Event table. In a way, the following structure could fit my needs.

class Event
  include DataMapper::Resource
  property :id, Serial
  property :begin, DateTime
end

class Talk
  include DataMapper::Resource
  property :id, Serial
  property :title, String
  belongs_to :event
  belongs_to :meeting
end

class Meeting
  include DataMapper::Resource
  property :id, Serial
  belongs_to :event
  has n, :talks
end

However, in order to use this, I would need to manually create an Event every time, I want to create or edit a Talk. That is, I can’t do talk.begin or Talk.create(:begin => Time.now). Is there a way around this without patching all functions and merging the properties? I don’t want to be reminded of the underlying structure when using the model.

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

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

发布评论

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

评论(1

你丑哭了我 2024-08-19 22:45:00

如果您想将事件的属性复制到谈话和会议中,那么您可以将其移动到模块中:

module EventFields
  def self.included(base)
    base.class_eval do
      include DataMapper::Resource
      property :id, DataMapper::Types::Serial
      property :begin, DateTime 
      # other fields here
    end
  end
end

class Talk
  include EventFields
  property :title, String
  belongs_to :meeting
end

class Meeting
  include EventFields
  has n, :talks
end

这将为您提供不同的表,但意味着减少重复。

If you want to replicate the attributes of Event into Talk and Meeting then you could move it into a module:

module EventFields
  def self.included(base)
    base.class_eval do
      include DataMapper::Resource
      property :id, DataMapper::Types::Serial
      property :begin, DateTime 
      # other fields here
    end
  end
end

class Talk
  include EventFields
  property :title, String
  belongs_to :meeting
end

class Meeting
  include EventFields
  has n, :talks
end

This will give you different tables but means duplication is reduced.

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