編集::child_key => [:comparison_id]
のコメントを参考にしてください。DataMapperで同じモデルに対して多対多の関係を2つ設定するにはどうすればよいですか?
私はこのように見える二つのモデルがあります:Msrunsの2セットを比較から来
class Comparison
include DataMapper::Resource
property :id, Serial
end
class Msrun
include DataMapper::Resource
property :id, Serial
property :name, String
end
の比較。私はこれを、比較からMsrunへの2つの多対多の関係を通して表現すると考えましたが、私はDataMapperでこれをどうやって行うかについて頭を悩ましています。しかし、これが唯一の二つのモデル間の1の多対多の関係を行います
has n, :whatevers, :through => Resource
:私は、多対多の関係がこのような何かを追加することで利用可能であることを知っています。
# Join model for the comparison-msrun many-to-many relationship.
class First
include DataMapper::Resource
belongs_to :msrun, :key => true
belongs_to :comparison, :key => true
end
# Join model for the comparison-msrun many-to-many relationship.
class Second
include DataMapper::Resource
belongs_to :msrun, :key => true
belongs_to :comparison, :key => true
end
class Comparison
include DataMapper::Resource
property :id, Serial
has n, :firsts
has n, :msrun_firsts, 'Msrun', :through => :firsts, :child_key => [:msrun_id]
has n, :seconds
has n, :msruns_seconds, 'Msrun', :through => :seconds, :child_key => [:msrun_id]
end
class Msrun
include DataMapper::Resource
property :id, Serial
property :name, String
has n, :firsts
has n, :comparison_firsts, 'Comparison', :through => :firsts, :child_key => [:comparison_id]
has n, :seconds
has n, :comparison_seconds, 'Comparison', :through => :seconds, :child_key => [:comparison_id]
end
次のエラーでautomigrate
結果実行:
rake aborted!
No relationships named msrun_firsts or msrun_first in First
何午前私はまた、二つのモデルに参加作成し、手動で関係を指定し、手動でそのような各関係の子キーを指定して試してみました私はここで間違っている?どうすればこの作品を作れますか?私はあなたが行うことができると信じてDataMapperのdocsあたり
として
申し訳ありませんが、私はより明確にすべきでした。私はそれを試して、次のエラーが表示されます:first_msrunsまたはfirst_msrunという名前の関係は最初にありません – jergason
命名エラーを避けるには、Msrunの 'n、:firsts、...:child_key => [:msrun_id] 'と':firsts、...:child_key => [:comparison_id] 'を比較します。 異なる名前のフィールドをバインドするには、 ':child_key'が必要です。 – ujifgc