2012-01-16 25 views
1

使用以下類及其關聯。如何正確訪問belongs_to模型的屬性

class Repository 
    include DataMapper::Resource 
    property :id, Serial 
    property :name, String 
    has n, :branches 
end 

class Branch 
    include DataMapper::Resource 
    property :id, Serial 
    property :note, String 
    belongs_to :repository 
end 

# Simple creation of a repository and branch belonging to said repository 
repo = Repository.new 
repo.name = "Bob" 
branch = repo.branches.new 
branch.note = "Example Note" 
repo.save 

# Print the repo->branch's note 
puts repo.branches.first.note # Prints "Example Note" 

# Print the branch->repo name 
puts branch.repository.first.name # Does not work 
puts branch.repository.name # Does not work 

我可以從(:Repository.first.branches.first.note前)跌訪問性能。

我似乎無法從Branch訪問屬性,從分支中獲取存儲庫的名稱(例如:Branch.first.repository.first.name)。


** **解決 原來,我不能實際使用我的類名的DataMapper已經將它(API)。解決方案是簡單地重命名我的課程,然後它按預期工作。

回答

2

由於DataMapper已經使用(API),所以不能使用類名稱存儲庫。解決方案是簡單地重命名類,然後它按預期工作。

相關問題