Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby Mongomapper阵列的多对多问题_Ruby_Associations_Mongomapper - Fatal编程技术网

Ruby Mongomapper阵列的多对多问题

Ruby Mongomapper阵列的多对多问题,ruby,associations,mongomapper,Ruby,Associations,Mongomapper,我想从用户的任务列表中列出该用户的所有任务名称 但当我使用下面的代码时,我得到以下信息: undefined method `task_id' for ... 以下是我的课程: class User include MongoMapper::Document key :name, String key :tasklist, Array # I need this to hold ObjIds many :tasks, :in => :tasklist, :class_n

我想从用户的任务列表中列出该用户的所有任务名称

但当我使用下面的代码时,我得到以下信息:

undefined method `task_id' for ...
以下是我的课程:

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  
我用过:

a = self.new
a.tasklist = [object1,object2]
a.save
然后,我可以使用以下方法遍历所有对象引用:

a = User.find(a.id)
a.tasks.each do |task|
  puts task.name
end

但现在还有另一个问题,mongomapper似乎跳过了重复项: