Ruby 如何正确访问属于型号';s属性
使用以下类及其关联Ruby 如何正确访问属于型号';s属性,ruby,datamapper,model-associations,ruby-datamapper,Ruby,Datamapper,Model Associations,Ruby Datamapper,使用以下类及其关联 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
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.branchs.first.note
)
我似乎无法从分支访问属性,无法从分支获取存储库的名称(例如:Branch.first.repository.first.name
)
**解决**
事实证明,我无法实际使用存储库,因为DataMapper已经使用了我的类名。解决方案是简单地重命名我的类,然后它就可以正常工作。您不能使用类名存储库,因为DataMapper已经在使用它了。解决方案是简单地重命名类,然后它就可以按预期的方式工作