How can I have two many-to-many relationships to the same model in DataMapper?

What you're observing, is the fact that relationships are stored in a set like object under the hood, more specifically, a set that uses the relationship's name as discriminator. So what happens in your case, is that the latter definition overwrites the former, as sets don't allow duplicate entries (and in our case, replace the older entry with the newer, for the set's purposes, identical one) There are practical reasons for this. It makes no sense to declare two supposedly different relationships on one model, but name them the same.

How would you distinguish them when trying to access them? This manifests itself in DM's implementation, where a method named by the relationship name gets defined on the Resource. So what DM ends up doing in your case of trying to add a duplicate to the set, is that it will just use the latter options to generate the implementation of that method.

Even if it were to accept duplicate relationship names, the latter relationship would lead to an overwritten/redefined version of the same method, thus leaving you with the same net effect As a consequence, you would need to define differently named relationships on your models. When you think about it, it really makes sense. To help DM with inferring the model to use, you can pass the model name (or the constant itself) as the 3rd parameter to the has method, or as the 2nd parameter for 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 Hope that helps!

What you're observing, is the fact that relationships are stored in a set like object under the hood, more specifically, a set that uses the relationship's name as discriminator. So what happens in your case, is that the latter definition overwrites the former, as sets don't allow duplicate entries (and in our case, replace the older entry with the newer, for the set's purposes, identical one). There are practical reasons for URL1 makes no sense to declare two supposedly different relationships on one model, but name them the same.

How would you distinguish them when trying to access them? This manifests itself in DM's implementation, where a method named by the relationship name gets defined on the Resource.So what DM ends up doing in your case of trying to add a duplicate to the set, is that it will just use the latter options to generate the implementation of that method. Even if it were to accept duplicate relationship names, the latter relationship would lead to an overwritten/redefined version of the same method, thus leaving you with the same net effect.

As a consequence, you would need to define differently named relationships on your models. When you think about it, it really makes sense. To help DM with inferring the model to use, you can pass the model name (or the constant itself) as the 3rd parameter to the has method, or as the 2nd parameter for 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 Hope that helps!

That gives me the following error: No relationships named first_msruns or first_msrun in First. – Jergason Jun 24 at 2:09.

As per the DataMapper docs I believe you can do: 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.

Sorry, I should have made it more clear. I have tried that, and I get the following error: No relationships named first_msruns or first_msrun in First – Jergason Jun 24 at 2:07 1 To avoid naming errors you must specify has n, :firsts, ... :child_key => :msrun_id for Msrun and has n, :firsts, ... :child_key => :comparison_id for Comparison. :child_key is needed to bind fields with different names.

– ujifgc Jun 24 at 8:06.

Handy when defining self referential many-to-many relationships like we saw above. Specifiy the :via option on many to many relationships. Self referential many to many relationships.

Able to provide "better" names for use in our domain models. But if you instead call . Subset of the association's query results.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions