DataMapper has_one问题

时间:2009-07-05 07:35:47

标签: ruby datamapper one-to-one

我无法在DataMapper中关联模型。它非常简单,但我可以理解。

所以,我有两张桌子:

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

抱歉,也许这很愚蠢。但我无法弄清楚我应该使用什么样的声明。

1 个答案:

答案 0 :(得分: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

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

相关问题