Skip to content

Instantly share code, notes, and snippets.

@banker
Created December 1, 2009 18:36
Show Gist options
  • Save banker/246514 to your computer and use it in GitHub Desktop.
Save banker/246514 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'mongo_mapper'
MongoMapper.database = 'test-poly'
class Company
include MongoMapper::Document
many :addresses, :as => :addressable
end
# Only including to show that this is not just a contrived example
class Person
include MongoMapper::Document
many :addresses, :as => :addressable
end
class Address
include MongoMapper::Document
key :addressable_id, ObjectId
key :addressable_type, String
belongs_to :addressable, :polymorphic => true
end
# In console:
a = Address.new
c = Company.new
a.addressable = c
c.addresses #=> [] <-- Should be a, but is not
# Seems to work after a saves
a.save
p c.addresses
# However, this works:
c.addresses << a
c.addresses #=> a
p c.addresses
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment