2013-10-24 94 views
0

兩類:多對多的關係創造了現有對象MongoDB中

class Investor 
    field :name 
    has_and_belongs_to_many :users 
end 

class User 
field :name 
has_and_belongs_to_many :investors 
end 

我已經有用戶和投資者,我不希望創建新的。什麼是inclusing像

@user=User.first 
@investor=Investor.first 
@inves_sec=Investor.last 

現在什麼是添加@investor和@invest_sec用戶ID的投資者,可我現在mongoid文檔中發現的命令來創建新的投資對象的方式的方式,我是不是能夠找到你可以使用現有對象的地方。

+0

我不遵循你的問題。 'investor.users.push(@investor)'? – WiredPrairie

+0

感謝它的運作,我在做@ investor.id這就是爲什麼它是一個問題,感謝您的評論。您可以將其發佈爲answer @ user.investors.push(@investor),然後是下一個。我會接受它。 –

回答

1

這是很容易,你可以這樣做:

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

這裏有很多例子的關係處理/引用各種場景here