0

I would like to have a model with different types of has n, for example:

class Blog
  include DataMapper::Resource

  property :id, Serial

  has 1, :owner   # of type user...
  has n, :authors # of type user...
end

class User
  include DataMapper::Resource

  property :id, Serial

  has n, :blogs # owns some number
  has n, :blogs # is a member of some number
end

I don't, however, want to use the Discriminator, since then I need to make new Owner or Author objects of old User objects and that would be ridiculous.

How can I best achieve this?

tekknolagi
  • 10,663
  • 24
  • 75
  • 119

1 Answers1

0

Try this:

class User
  include DataMapper::Resource

  property :id, Serial

  has n, :blog_authors, 'BlogAuthor'
  has n, :authored_blogs, 'Blog', :through => :blog_authors, :via => :blog

  has n, :blog_owners, 'BlogOwner'
  has n, :owned_blogs, 'Blog', :through => :blog_owners, :via => :blog
end

class Blog
  include DataMapper::Resource

  property :id, Serial

  has n, :blog_authors, 'BlogAuthor'
  has n, :authors, 'User', :through => :blog_authors

  has 1, :blog_owner, 'BlogOwner'
end

class BlogAuthor
  include DataMapper::Resource

  belongs_to :blog, :key => true
  belongs_to :author, 'User', :key => true
end

class BlogOwner
  include DataMapper::Resource

  belongs_to :blog, :key => true
  belongs_to :owner, 'User', :key => true
end
ujifgc
  • 2,215
  • 2
  • 19
  • 21