我已经非常彻底地阅读了这个页面:
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