[Rails] Rails namespace help requested

zdennis zdennis at mktec.com
Wed Dec 14 17:14:10 GMT 2005


Michael Engelhart wrote:
> HI Zach -
> 
> THanks very much for your help.    I'm not sure I understand why my
> clash exists though because I have this exact scenario:
> 
> class StateProvince < ActiveRecord::Base
> end
> 
> # this is in a directory of lib/ws
> module WebServiceModule
>    class StateProvince
>    end
> end
> 
> Isn't the  namespace declared for the class inside the module as
> ::WebServiceModule::StateProvince and the Rails model as
> ::StateProvince?   Aren't those different objects in the eyes or Ruby?

Yes.

> 
> Also I'm not doing inheritance in my WebServiceModule, I'm just
> declaring a namespace right?

In your WebServiceModule you aren't doing any explicit inheritance. From your post from yesterday:

    but when I run my code in Rails, I get a superclass mismatch error
    when I include the module.

If you include you WebServiceModule in your top level, it will have a problem when it finds the 
StateProvince model. By saying:

    include WebServiceModule

It will move WebServiceModule::StateProvince (and all other constants) to scope that your "include" 
statement is in. In this case it is in the toplevel/global scope and it will be accessible as 
::StateProvince.

So now that it is toplevel accessible and ruby finds your StateService model which tries to subclass 
ActiveRecord::Base you get your superclass problem!

I hope this helps!

> 
> I'm a bit new to Ruby so it's possible I'm mixing this up but this is
> what I understood from reading the pickaxe book.

ooh, good book. =)

Zach


More information about the Rails mailing list