1
次のクラスとその関連付けを使用します。belongs_toモデルのプロパティに正しくアクセスする方法
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.first.repository.first.name
)。
** **解決しよう 私はDataMapperのように私のクラス名はすでにそれ(API)を使用していない実際の使用リポジトリできるようことが判明しました。解決策は単に私のクラスの名前を変更して、すべてのものが意図どおりに動作するようにすることです。