【发布时间】:2011-11-05 16:28:51
【问题描述】:
我已经非常彻底地阅读了这个页面:
http://datamapper.org/docs/associations
如果答案就在那里,那根本不是以我能理解的方式表达的。
我对通过 Datamapper 使用设置关系感到非常困惑。上面的 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
【问题讨论】:
标签: ruby datamapper ruby-datamapper