Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 如何正确访问属于型号';s属性_Ruby_Datamapper_Model Associations_Ruby Datamapper - Fatal编程技术网

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已经在使用它了。解决方案是简单地重命名类,然后它就可以按预期的方式工作