次のクラスとその関連付けを使用します。
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)を使用しているため、 Repositoryをクラス名として実際に使用できないことがわかりました。解決策は、クラスの名前を変更するだけで、すべて意図したとおりに機能します。