2016-06-18 170 views
0

我是使用DataMapper的新手,只想将一个类的属性添加到另一个类的表中。我有两个类,如下所示:我想添加'user'类中的'handle'属性作为'peep'表中的列。我已经要求所有相关的gem(不包括在下面),但我正在努力使用DataMapper语法。我尝试了has n, :userbelongs to等的变体,但都导致'user_id has NULL values'错误。DataMapper将另一个类的属性添加到另一个类的表中。

类 '用户':

class User 

     include DataMapper::Resource 

     property :id,    Serial 
     property :email,   String, format: :email_address, required: true, unique: true 
     property :handle,   String, required: true 
    end 

类 '窥视':

class Peep 

    include DataMapper::Resource 

    property :id,   Serial 
    property :peep_content, String 
    property :created_at, DateTime 

end 

回答

0

好像复制你的另一个问题:Data Mapper Associations - what code?

但这里是我的回答,联想键时不是传统的id..._id,您必须在添加关联时明确指定它,让DataMapper知道如何查询你的关系。

Doc:http://datamapper.org/docs/associations.html定制关联部分。

class User 
    ... 
    has n, :peeps, 'Peep', 
    :parent_key => [ :handle ],  # local to this model (User) 
    :child_key => [ :user_handle ] # in the remote model (Peep) 
end 

class Peep 
    ... 
    belongs_to :user, 'User', 
    :parent_key => [ :handle ],  # in the remote model (Peep) 
    :child_key => [ :user_handle ] # local to this model (User) 
end 
相关问题