2012-01-16 2 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.first.repository.first.name)에서 저장소 이름을 가져 오는 지점에서 속성에 액세스 할 수 없습니다.


** **이 해결 내가 DataMapper로 내 클래스 이름이 이미 (API)를 사용하지 실제 사용 저장소 수있는 것으로 밝혀졌습니다. 해결책은 단순히 내 수업의 이름을 바꾼 다음 모든 것이 의도 한대로 작동하도록하는 것입니다.

답변

2

DataMapper가 이미 (API)을 사용하므로 리포지토리을 사용할 수 없습니다. 해결책은 단순히 클래스의 이름을 바꾼 다음 모든 것이 의도 한대로 작동하도록하는 것입니다.