tags:

views:

33

answers:

1
class Person
  include Mongoid::Document
  field :name
  embeds_many :addresses
end

class Company
  include Mongoid::Document
  field :name
  embeds_many :addresses
end

class Address
  include Mongoid::Document
  embedded_in :addressable, inverse_of :addresses
end

I tried something like this

company = Company.first
person = Person.first
address = Address.new

company.addresses << address
company.save
=>true

person.addresses << address
person.save
=>true

But I didn't found address embedded in person.But I found that it was embedded in company. Did anyone know why? Or Can't I embed address in multiple document.

Again while I reversed like this

person.addresses << address
person.save
=>true

company.addresses << address
company.save
=>true

I found address was embedded in person not in company.. Any Ideas.

A: 

Try to clone your address :

person.addresses << address
person.save
=>true

company.addresses << address.clone
company.save
=>true

All document even embedded are _id so it not new_record in second case if you ton clone it.

shingara
Thanks for quick response. While I tried Person.find(person_id).addresses it gave me address. But it didn't give me address when trying Company.find(company_id).addresses after cloning the address object too. I was supposed to get person and company object when doing only Person.find(person_id).addresses.first.addressable. Is there is any solution?
kshil