Deepender Singla
Deepender Singla

Reputation: 997

Many to Many relationship creation for existing object in mongodb

Two classes:

class Investor
  field :name
  has_and_belongs_to_many :users
end

class User
 field :name
 has_and_belongs_to_many :investors
end

I already have users and investors I don't want to create new. What is the way of inclusing like

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

Now what is the way to add @investor and @invest_sec to the user investors id, all the command I found in mongoid doc create new investor object, I wasn't able to find thorugh which you can use existing object.

Upvotes: 0

Views: 99

Answers (1)

WiredPrairie
WiredPrairie

Reputation: 59783

It's easy enough as you could do something like:

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

There are a lots of examples here of various scenarios dealing with relationships/references.

Upvotes: 1

Related Questions