Mongomapper many to many issue with Array

a 夏天 提交于 2019-12-02 15:55:57

问题


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  

回答1:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!