正确的方式来建立DataMapper关联

时间:2010-09-25 18:58:27

标签: ruby datamapper

我想要一张用户表。这些用户应有n个联系人和n个消息..

我的代码是:

...
class User
  include DataMapper::Resource

  property :id, Serial, :key => true
  property :nickname, String

  has n, :contacts
  has n, :messages
end

class Contact
  include DataMapper::Resource

  belongs_to :user

  property :id, Serial, :key => true
  property :authgiven, String

  has 1, :user     
end

class Message
  include DataMapper::Resource

  belongs_to :user

  property :id, Serial, :key => true
  property :data, String
end

#apply models (validation etc.)
DataMapper.finalize
...

初始化DataMapper没有错误,但是当我尝试创建一个新用户或者其他什么时,save总是返回false ...有人可以指出出了什么问题吗?

我对DataMapper很陌生,它总是适用于没有关系的简单表格,所以我认为它与我宣布1:n关系的方式有关...

1 个答案:

答案 0 :(得分:1)

嘿,你应该删除那个有来自Contact模型的1,:用户行,你应该很好。

相关问题