Aug 17, 2011

Mongoid cannot have same type of embeds_many

If you have multiple embeds_many which are of the same class, every array you save will be on the first field.


embeds_many :agree_names, :as => :notificator, :class_name => "BaseNotificator"
embeds_many :disagree_names, :as => :notificator, :class_name => "BaseNotificator"


In order to prevent this, just change :as to be different. And do not forget to change embedded_in.


embeds_many :agree_names, :as => :agree_notificator, :class_name => "BaseNotificator"
embeds_many :disagree_names, :as => :disagree_notificator, :class_name => "BaseNotificator"


And in the embedded class, do this:


embedded_in :agree_notificator, :polymorphic => true
embedded_in :disagree_notificator, :polymorphic => true