使用以下类及其关联。
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
。
** 已解决 ** 结果我不能实际使用Repository作为我的类名,因为 DataMapper 已经使用它(API)。解决方案是简单地重命名我的类,然后一切都按预期工作。