Suppose we've two models, Task and User. So a person might have many tasks and tasks should have the ability to have numerous customers too. But, an activity should in addition have a unique creator who is another user.

Exemple:

An activity within this context is much like this: Task ID, Task Creator, Customers who must do the job

User_1 produces an activity and that he will be the creator. User_1 identifies User_2 and User_3 as customers who must do the job. So both of these last customers aren't designers of task.

How do you create this models to ensure that if I've got a task object, I'm able to find it's creator and customers who should complete it. And just how will i do, if I've got a user, to locate all tasks he produced and all sorts of tasks he should complete.

You will need a many-to-many relationship between your Tasks and Customers, and also you need one more one-to-many relationship between Customers and Tasks, pointing towards the creator (User).

Something along wrinkles: (It's my job to use Mongoid, so double-look into the syntax for that relations within the MongoMapper API - link below.. you may to by hand specify :foreign_key and :class)

The concept is you have two associations between your models, the one that models the numerous-to-many relationship that you receive with the idea to the assigned_users or assigned_tasks, along with a one-to-many relationship that you're able to either the creator of the task, or even the created_tasks for any given user. Should you chose these names for that associations, it will likely be obvious that is which.

class Task
  include MongoMapper::Document
  key :title, String , :required => true

  key :user_ids , Array
  has_many :users, :in => user_ids     # , :as => :assigned_users

  key :creator_id , ObjectId
  belongs_to: user, :as => :creator

end

class User
  include MongoMapper::Document
  key: name, String, :required => true

  has_many :tasks           # , :as => :assigned_tasks

  has_many :tasks, :as => :created_tasks
end

See:

http://mongomapper.com/documentation/plugins/associations.html

The solution recommended by Tilo is correct concerning how to model the information, however the example code is incorrect and won't work. The :as choice is for polymorphic associations, you need to make use of the :foreign_key option. Also, you cannot have two associations named exactly the same. See below for modified code.

class Task
  include MongoMapper::Document
  key :title, String , :required => true

  key :assigned_user_ids, Array
  has_many :assigned_users, :in => :assigned_user_ids

  key :creator_id , ObjectId
  belongs_to :creator, :class => User
  # userstamps! also accomplishes the above
end

class User
  include MongoMapper::Document
  key: name, String, :required => true

  has_many :created_tasks, :foreign_key => :creator_id, :class => Task

  # inverse of many :in is still in the works
  # see https://github.com/jnunemaker/mongomapper/pull/259
  # this is a decent workaround for now
  def assigned_tasks 
    Task.where(:assigned_user_ids => self.id)
  end
end

See also: