2016-06-18 168 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 
相關問題