Permalink
Browse files

Initialize the relationships after loading all the models

* This solves a very common issue caused by loading models in different
  orders that depend on each other to be loaded before the association
  can be initialized.
  • Loading branch information...
1 parent 51cb1e9 commit 3c4814da849cd8970b87c1015229fe01ee644547 Dan Kubb committed Oct 5, 2008
Showing with 18 additions and 0 deletions.
  1. +18 −0 merb_datamapper/lib/merb_datamapper.rb
@@ -33,6 +33,24 @@ def self.run
end
end
+ class Merb::Orms::DataMapper::Associations < Merb::BootLoader
+ after Merb::BootLoader::LoadClasses
+
+ def self.run
+ Merb.logger.debug 'Merb::Orms::DataMapper::Associations block.'
+
+ # make sure all relationships are initialized after loading
+ descendants = DataMapper::Resource.descendants.dup
+ descendants.each do |model|
+ descendants.merge(model.descendants) if model.respond_to?(:descendants)
+ end
+ descendants.each do |model|
+ model.many_to_one_relationships.each { |r| r.child_key }
+ end
+
+ Merb.logger.debug 'Merb::Orms::DataMapper::Associations complete'
+ end
+ end
generators = File.join(File.dirname(__FILE__), 'generators')
Merb.add_generators generators / 'data_mapper_model'

0 comments on commit 3c4814d

Please sign in to comment.