0

I've got two models, that link to each other.

class User
  include DataMapper::Resource

  property :id, Serial
  has n, :mail_addresses
end

class MailAddress
  include DataMapper::Resource

  property :id, Serial
  property :email, String, :required => true, :unique => true, :format => :email_address

  belongs_to :user
end

Now I want to add a primary mail address to a user. So it can do things like

some_user.primary_mail_address = some_user.mail_addresses.first

I've been trying to do things like this on the user model, but without any luck.

property :primary_mail_address_id, Integer, required: false
has 1, :primary_mail_address, model: 'MailAddress', parent_key: [:primary_mail_address_id], child_key: [:id]

The above trick allows me to execute the code sample, but the primary_mail_address_id won't get updated when I do that.

How to do it?

1 Answers1

0

Found own solution.

The problem is that I confused has 1 with belongs_to. has 1 is actually a one_to_many relationship that tries to create a property on the MailAddress model, instead of on the User model.

The trick was:

belongs_to :primary_mail_address, MailAddress, required: false