Ruby Datamapper 表继承与关联
我开始学习 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
列,显然,鉴别器列几乎没有什么作用这里的值,因为从数据库视图来看,Talk
和 Meeting
应该有单独的表。
因此,最后,我希望 Talk
和 Meeting
共享 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.begin
或 Talk.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 Event
s 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想将事件的属性复制到谈话和会议中,那么您可以将其移动到模块中:
这将为您提供不同的表,但意味着减少重复。
If you want to replicate the attributes of Event into Talk and Meeting then you could move it into a module:
This will give you different tables but means duplication is reduced.