mongodb中现有对象的多对多关系创建

时间:2013-10-24 14:41:03

标签: ruby-on-rails-3 mongodb mongoid mongoid3

两个班级:

class Investor
  field :name
  has_and_belongs_to_many :users
end

class User
 field :name
 has_and_belongs_to_many :investors
end

我已经有了用户和投资者,我不想创造新的。包含类似

的方式是什么?
@user=User.first
@investor=Investor.first
@inves_sec=Investor.last

现在将@investor和@invest_sec添加到用户投资者ID的方法是什么,我在mongoid doc中找到的所有命令都创建了新的投资者对象,我无法找到可以使用现有对象的东西。

1 个答案:

答案 0 :(得分:1)

这很容易,因为你可以这样做:

@first_investor = Investor.first
@user.investors.push(@first_investor)

关于关系/参考的各种场景有很多例子here

相关问题