Trevor Squires
2006-Feb-02 03:21 UTC
proposed bugfix/patch: ActiveRecord cross-module associations
Hi, I''m still test driving the improved support for module namespaces in rails and I''ve hit a problem with associations between AR classes in modules and AR classes in the root namespace. The long and short of it is I want to be able to say: Foo::Bar.belongs_to :wibble, :class_name => "::Wibble" If you don''t try to prefix ''Wibble'' with ''::'' it complains about uninit constant "Foo::Wibble" which completely makes sense. However, the AR::Base.type_name_with_module method assumes that *all* names belong in (for example) "Foo" and spits out "Foo::::Wibble". I''ve included a patch below that solves the problem by treating type_names that are prefixed with ''::'' differently. I''d appreciate any feedback (or a note saying the patch was applied :- P ) Regards, Trevor -- Trevor Squires http://somethinglearned.com Index: vendor/rails/activerecord/lib/active_record/base.rb ==================================================================--- vendor/rails/activerecord/lib/active_record/base.rb (revision 4379) +++ vendor/rails/activerecord/lib/active_record/base.rb (working copy) @@ -922,6 +922,7 @@ # Returns the name of the type of the record using the current module as a prefix. So descendents of # MyApp::Business::Account would appear as "MyApp::Business::AccountSubclass". def type_name_with_module(type_name) + return type_name.sub(/^::/,'''') if type_name =~ /^::/ self.name =~ /::/ ? self.name.scan(/(.*)::/).first.first + "::" + type_name : type_name end