使用以下类及其关联。
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)。解决方案是简单地重命名我的类,然后它按预期工作。