【问题标题】:DataMapper: one to many relationship with one of several models?DataMapper:与几个模型之一的一对多关系?
【发布时间】:2013-05-28 19:48:48
【问题描述】:

这是我的模型:

class Item
  include DataMapper::Resource

  property :id, Serial

  has 1, :firstitem
  has 1, :seconditem
end

class FirstItem
  include DataMapper::Resource

  property :id, Serial

  belongs_to :item
end

class SecondItem
  include DataMapper::Resource

  property :id, Serial

  belongs_to :item
end

现在,我的问题是 - 如果我希望 FirstItemSecondItem 成为不同的模型,但希望它们都可能成为 Item 的一部分(但只有两者之一,所以记录 @987654325 @ 也不会有 SecondItem),我可以为它们创建 has 1 关系,并且只有其中一个被填充。

那么在关系数据库中,这样做有意义吗?有没有更好、更有效的方法来定义这种关系?

【问题讨论】:

    标签: ruby orm sinatra datamapper


    【解决方案1】:

    您想要的是多态关联,遗憾的是,DataMapper 不支持。尝试查看 ActiveRecord;您可以轻松地将它与 Sinatra 一起使用。

    【讨论】:

    • 谢谢,知道这很有用!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多