0

我在 DataMapper 中关联模型时遇到问题。它真的很简单,但我能明白。

所以,我有 2 张桌子:

1. Books
-> id
-> title
-> publisher_id

2. Publishers
-> id
-> title

课程:

class Book
  property :id, Serial
  property :title, String
  property :publisher_id, Integer
end

class Publisher
  property :id, Serial
  property :title, String
end

所以,问题是:我如何将出版商连接到书?这是一对一的关系,整个事情应该是这样的:

p = Book.get(12345).publisher

对不起,也许这是愚蠢的。但我只是不知道我应该使用什么样的声明。

4

1 回答 1

2

哈哈,我是凌晨 2 点的傻白痴。总是发生在我身上,当我问这个问题时——我自己突然为我的问题找到了答案。

这是不正确的,存在一对多的关系。所以,它就像天空中的太阳一样简单:

class Book
  property :id, Serial
  property :title, String
  property :publisher_id, Integer

  belongs_to :publisher
end

class Publisher
  property :id, Serial
  property :title, String

  has n, :books
end

就是这样。这可能对某人有帮助。

于 2009-07-05T07:40:31.370 回答