2011-06-23 7 views
0

編集::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あたり

として

答えて

0

class Msrun 
    include DataMapper::Resource 
    property :id, Serial 
    property :name, String 

    has n, :firsts #This line could probably be omitted 
    has n, :first_comparisons, 'Comparison', :through => :firsts 

    has n, :seconds #This line could probably be omitted 
    has n, :second_comparisons, 'Comparison', :through => :seconds 
end 
+0

申し訳ありませんが、私はより明確にすべきでした。私はそれを試して、次のエラーが表示されます:first_msrunsまたはfirst_msrunという名前の関係は最初にありません – jergason

+1

命名エラーを避けるには、Msrunの 'n、:firsts、...:child_key => [:msrun_id] 'と':firsts、...:child_key => [:comparison_id] 'を比較します。 異なる名前のフィールドをバインドするには、 ':child_key'が必要です。 – ujifgc

0

あなたが見ているものは、関係がより具体的には、ボンネットの下にオブジェクトのようなセットに格納されているという事実でありますその関係の名前を弁別子として使用する集合。だから、あなたのケースでは何が起こるのでしょうか?後者の定義は前者を上書きします。セットは重複したエントリを許可しません(私たちの場合は古いものを新しいものに置換します。

これには実用的な理由があります。 1つのモデルで2つの異なる関係を宣言するのは意味がありませんが、同じ名前を付けます。あなたがそれらにアクセスしようとしているとき、それらをどのように区別しますか?これはDMの実装で明らかになり、リレーションシップ名で指定されたメソッドがリソース上で定義されます。ですから、セットに複製を追加しようとした場合、DMはそのメソッドの実装を生成するために後者のオプションを使用するだけです。たとえそれがリレーションシップ名を重複して受け入れるとしても、後者の関係は同じメソッドの上書き/再定義されたバージョンにつながり、同じネットエフェクトを残します。

結果として、モデルに異なる名前の関係を定義する必要があります。あなたがそれについて考えるとき、それは本当に理にかなっています。使用するモデルを推定してDMを支援するために、あなたはhasメソッドに3番目のパラメータとしてモデル名(または定数自体)を渡すことができ、または助けbelongs_to

class Comparison 
    include DataMapper::Resource 
    property :id, Serial 

    has n, :firsts 
    has n, :first_msruns, 'Msrun', :through => :firsts 

    has n, :seconds 
    has n, :second_msruns, 'Msrun', :through => :seconds 
end 

class Msrun 
    include DataMapper::Resource 
    property :id, Serial 
    property :name, String 

    has n, :firsts 
    has n, :first_comparisons, 'Comparison', :through => :firsts 

    has n, :seconds 
    has n, :second_comparisons, 'Comparison', :through => :seconds 

end 

ホープ第二パラメータとして!

+0

このコードは正常に機能しますか?それは私に次のエラーを与えます:最初にfirst_msrunsまたはfirst_msrunという名前の関係はありません。 – jergason

関連する問題