Domanda

Utilizzando le seguenti classi e le loro associazioni.

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

Posso accedere alle proprietà da Repository (es: Repository.first.branches.first.note).

Non riesco ad accedere alle proprietà da Branch , ottenendo il nome del repository da un ramo (es: Branch.first.repository.first.name).


** RISOLTO ** Risulta che non posso effettivamente utilizzare Repository come nome della mia classe poiché DataMapper lo utilizza già (API) .La soluzione è semplicemente rinominare la mia classe e poi tutto funziona come previsto.

È stato utile?

Soluzione

Non è possibile utilizzare il nome della classe Repository poiché DataMapper lo utilizza già (API) .La soluzione è semplicemente rinominare la classe e poi tutto funziona come previsto.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top