I would like to list all the task names for the User form the user's tasklist
But when I use the code below I get the following message:
undefined method `task_id' for ...
Here are my classes:
class User
include MongoMapper::Document
key :name, String
key :tasklist, Array # I need this to hold ObjIds
many :tasks, :in => :tasklist, :class_name => 'Task'
def add(taskid)
a = self.new
a.task_id << taskid
a.save
end
class Task
include MongoMapper::Document
key :name, String
many :users
end
I used:
a = self.new
a.tasklist = [object1,object2]
a.save
Then I can iterate through all the object references using:
a = User.find(a.id)
a.tasks.each do |task|
puts task.name
end
来源:https://stackoverflow.com/questions/24724393/mongomapper-many-to-many-issue-with-array