Hi!
I have some problems and some questions about usage of data mapper in
my project! Probably they are all know I hope someone can help with
some answer. I prepared all the examples below as ruby scripts which
can be found in the Files section of this group. The file is named
"manyToManyAssociationProblems.zip"
1. I need more than one one-to-many association from one class to a
second class. Below is an example how I solved the problem but am not
sure if this is how the child_key option is meant to be used. My
question is how do I define what is the second side of an association
if I have more then one association between two classes?
class Partner
include DataMapper::Resource
property :id, Integer, :serial => true
property :name, String
has n, :ownedConracts , :class_name => 'Contract', :child_key =>
[:owner_id]
has n, :paidConracts , :class_name => 'Contract', :child_key =>
[:payer_id]
end
class Contract
include DataMapper::Resource
property :id, Integer, :serial => true
property :number, Integer
belongs_to :owner, :class_name => 'Partner', :child_key =>
[:owner_id]
belongs_to :payer, :class_name => 'Partner', :child_key =>
[:payer_id]
end
2. Another question are association many-to-many. I have tried out
different scenarios but without success. I will try to show them on
examples so my problem will be more exactly described.
2.1. Using modules and many to many associations
Example code can be found in attached file: “manyToMany_withModuls.rb”
The first problem I found is that usage of modules is not supported
properly when using many-to-many references.
Example:
module A
module B
class Partner
include DataMapper::Resource
property :id, Integer, :serial => true
property :name, String
has n, :contractsReviewed, :through => Resource, :mutable =>
true, :class_name => 'Contract'
end
class Contract
include DataMapper::Resource
property :id, Integer, :serial => true
property :number, Integer
has n, :reviewedBy, :through => Resource, :mutable =>
true, :class_name => 'Partner'
end
end
end
If I tried to use such a model I always got the error: »Cannot find
the child_model AbPartnerContract for A::B::Partner«. I also tried to
change the :class_Name option to fully qualified names (A::B::Partner
and A::B::Contract) but there was no difference.
2.2. Many to many through Resource insert error
Example code can be found in attached file: “manyToMany.rb” test:
“testInsertError”
Now if I left out the modules the problem disappeared and Datamapper
worked on the fist glace. So the model was:
class Partner
include DataMapper::Resource
property :id, Integer, :serial => true
property :name, String
has n, :contractsReviewed, :through => Resource, :mutable =>
true, :class_name => 'Contract'
end
class Contract
include DataMapper::Resource
property :id, Integer, :serial => true
property :number, Integer
has n, :reviewedBy, :through => Resource, :mutable =>
true, :class_name => 'Partner'
end
But then again after a closer look this did not work correctly if I
tried to store more relationship between more object at once it again
did not work.
That I tried to do is create two partner and two contracts and define
that each contract was reviewed by each partner.
The Example below shows that I mean:
partner = Partner.new
partner.name= 'Peter'
partner2 = Partner.new
partner2.name= 'Matevž'
contract = Contract.new
contract.number= 1
contract2 = Contract.new
contract2.number= 2
partner.contractsReviewed << contract
contract.reviewedBy << partner
partner.contractsReviewed << contract2
contract2.reviewedBy << partner
partner2.contractsReviewed << contract
contract.reviewedBy << partner2
Whe I commit this i get the error: INSERT INTO
"contracts_partners" ("contract_id ", " partner_id ") VALUES (1, 2).
This error of courese does not make sence becuse we did not inset
doubled values into the intermediate (relationship) tabble.
If we leave out the last to lines the intrmediate table
CONTRACT_PARTNERS loks like:
partner_id contract_id
1 1
1 2
And there should be no problem inserting the abowe mentioned values.
2.3. Many to many through Resource not all Associations Inserted
Example code can be found in attached file: “manyToMany.rb” test:
“testNotAllAssociationsInserted”
Another strange thing is that if we in the last two lines chage the
contrat which is being assignet to the second contract created the
code runs thrue but the intermediate tabe is worg:
The code:
partner = Partner.new
partner.name= 'Peter'
partner2 = Partner.new
partner2.name= 'Matevž'
contract = Contract.new
contract.number= 1
contract2 = Contract.new
contract2.number= 2
partner.contractsReviewed << contract
contract.reviewedBy << partner
partner.contractsReviewed << contract2
contract2.reviewedBy << partner
partner2.contractsReviewed << contract2
contract2.reviewedBy << partner2
The tabele CONTRACT_PARTNERS actually crated contains only two lines:
partner_id contract_id
1 1
2 2
We lost the case where the contratct 2 was review by partner 1. The
exected table would look like:
partner_id contract_id
1 1
1 2
2 2
So this is probably a bug.
2.4. Many to many with defined intermediate table
Example code can be found in attached file:
manyToMany_with_intermediate_table.rb.
After few days trying and playing around with many to many
relationship with default intermediate table (:through => Resource)
like explained in examples above I tried my example for many to many
relationship with self defined intermediate table and again without
success. Can anybody helps me and explains how many to many
relationship with intermediate table should be defined (I am confused
with different options you can apply at definition of association).
Please see my example below and help me with explanation:
class Review
include DataMapper::Resource
property :contract_id, Integer, :key => true
property :partner_id, Integer, :key => true
belongs_to :partner, :child_key => [:partner_id], :class_name =>
"Partner"
belongs_to :contract, :child_key => [:contract_id], :class_name =>
"Contract"
end
class Partner
include DataMapper::Resource
property :id, Integer, :serial => true
property :name, String
has n, :reviews
has n, :contractsReviewed, :through => :reviews, :mutable =>
true, :class_name => 'Contract', :child_key =>
[:partner_id], :remote_name => :contract
end
class Contract
include DataMapper::Resource
property :id, Integer, :serial => true
property :number, Integer
has n, :reviews
has n, :reviewedBy, :through => :reviews, :mutable =>
true, :class_name => 'Partner', :child_key =>
[:contract_id], :remote_name => :partner
end
Thanks for the help.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"DataMapper" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/datamapper?hl=en
-~----------~----~----~----~------~----~------~--~---