1

我已经非常彻底地阅读了这个页面:

http://datamapper.org/docs/associations

如果答案就在那里,那根本就不是以我能理解的方式表达的。

我对通过 Datamapper 使用建立关系感到非常困惑。上面的数据映射器站点几乎是我能找到的关于该主题的所有内容,正如我所说,它并不是特别有用。

例如,如果我想创建如下内容:

桌子:users

id (primary key)
name

桌子:attributes

id (pk)
title

桌子:user_attributes

id (pk)
user_id (fk to users.id)
attribute_id (fk to attributes.id)
value

这看起来很简单,但它却非常困难。我尝试的一切都会给我错误,比如No relationships named user_attributes or user_attribute in UserUserAttribute (DataMapper::UnknownRelationshipError)

有人可以告诉我这个简单映射的类定义,或许可以让我更好地讨论 DataMapper 关联吗?以下是我尝试过的一些内容。

class User
  include DataMapper::Resource

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

  has n, :user_attributes, :through=>:attribute
end

class Attribute
  include DataMapper::Resource

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

  has n, :user_attributes, :through=>:user
end

class UserAttribute
  include DataMapper::Resource

  belongs_to :user
  belongs_to :attribute
end
4

2 回答 2

0

我认为您看到的是UserUserAttribute因为 DataMapper 正在尝试自动生成匿名连接类。

这是一篇文章,描述了如何在 DataMapper 中建立命名的多对多关系

您可能会像这样更改示例:

  • 用户 -> 用户
  • 项目 -> 属性
  • 协作 -> 用户属性
于 2011-08-28T23:46:50.583 回答
0

问题是您的类是 UserAttribute,并且您尝试使用 user_attributes 命名关系。去掉下划线,你的问题就会消失(或者给类名加下划线)

于 2015-04-25T08:22:58.163 回答