I am not sure if what I want to do is wrong or just my implementation.
what I want to do is
have multiple “has 1” relationships between a person and addresses
my models are
class Person
include DataMapper::Resource
property id,serial
property name,String
has 1, :home, :model => 'Address'
has 1, :office, :model => 'Address'
has 1, :mail, :model => 'Address'
end
class Address
include DataMapper::Resource
property :id,Serial
property addr1, String
property country, String
end
This works fine in code and I can assign and access country of a person
a_person.home.country
but when I save then retireve from DB it does not work. It mixes up home, office and mail addresses
I was hoping to get a structure like
CREATE TABLE `addresses` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`addr1` varchar(50) DEFAULT NULL,
`country` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
)
CREATE TABLE `persons` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(50) DEFAULT NULL,
'home' int(10) unsigned ,
'office' int(10) unsigned ,
'mail' int(10) unsigned ,
PRIMARY KEY (`id`)
)
what I got was
CREATE TABLE `addresses` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`addr1` varchar(50) DEFAULT NULL,
`country` varchar(50) DEFAULT NULL,
`person_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `index_addresses_person` (`person`)
)
CREATE TABLE `persons` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
)
the table structure is not important to me, what I want is that home,office and mail are all the same object type and perferrably are in the same table. It would be nice to be able assign the same Address instance to say, both home and mail and have it stored only once in addresses but referenced twice.
P.S. people and addresses are not the actual underlying object they are just used as a familiar example.
Edit: I may be able to do what I want by putting 4 has n assoc on Address. I will try and edit my answer with the results.
Actually you might want something like this: