Reputation: 2130
I am using mongomapper and I am saving association using the following method:
class Task
include MongoMapper::Document
key :user_id, ObjectId #also works without this line
belongs_to :user
def self.add(user)
a = self.new
a.user_id = user
a.save
end
And in the User model I have added: many :Tasks
Now, I would like to save two users (in the html form I select 2 users from the Users collection), without using the array, I want to save them separately:
class Task
include MongoMapper::Document
key :from_user_id, ObjectId # user1 links to the Users model
key :to_user_id, ObjectId # user2 links to the Users model
How od I do that?
Upvotes: 0
Views: 42
Reputation: 34072
MongoMapper has similar options as ActiveRecord when it comes to specifying keys and class names. You'd do something like:
class Task
include MongoMapper::Document
key :to_user_id, ObjectId
key :from_user_id, ObjectId
belongs_to :from_user, class_name: 'User', foreign_key: :from_user_id
belongs_to :to_user, class_name: 'User', foreign_key: :to_user_id
end
Upvotes: 1