Namespaces problem

I’m working on a module based application.
The first module is an eCommerce module.

I’ve made a few models for the webshop with the namespace Webshop:
Webshop::Category
Webshop::Product
etc.

My backend application has the namespace Admin::ModuleName::Controller.
Now if I call one of the webshop models from my catalog controller
(Admin::Webshop::CatalogController) i get the following error:

uninitialized constant Admin::Webshop::CatalogController::Webshop

I’ve tried to call the model on various ways like Category.find and
Webshop::Category.find both giving me the same error.

If I rename the model to Admin::Webshop::Category everything works fine
but this is not what I want because I have to call the models from the
front-end site too.

Thanks in advance

Wow that looks almost like DOUBLE namespacing!

map.namespace(:admin) do |admin|
admin.namespace(:webshop) do |webshop|
webshop.resources :categories
end
end

Please! Don’t namespace your models as they don’t need to be namespaced!
I
don’t care what script/generate scaffold does, it’s WRONG.

Ryan B. wrote:

Wow that looks almost like DOUBLE namespacing!

map.namespace(:admin) do |admin|
admin.namespace(:webshop) do |webshop|
webshop.resources :categories
end
end

Please! Don’t namespace your models as they don’t need to be namespaced!
I
don’t care what script/generate scaffold does, it’s WRONG.

Thank you very much.
About namespacing models;
Is there a better way to classify my models for each module?
Because later i want to create a CMS module, this module will also have
a model Category just like my webshop.
Any sugesstions?

Are you building the commerce stuff from scratch? If so, you might
want to check out Spree (http://spreehq.org). If not, I’d be curious
to know what other solutions you are using.

Sean

Jake J Jacobsen wrote:

Danny,

Webshop::Category.find is the correct way to call your namespaced
Rails models. I’m not sure exactly what your problem is, but there are
a few things to check.

  1. Make sure the namespaced model is defined correctly, it should be:

class Webshop::Category < ActiveRecord::Base
self.table_name = ‘webshop_categories’
end

I’ve found that you have to explicitly give it the table name.

  1. Make sure all of your model’s files are in a folder with with your
    namespace name. For example:

/models/webshop/categories.rb

Check those things and let me know if you are still having problems.

-Jake

On May 4, 3:04 am, Danny H. [email protected]

Thanks Jake,

The namespaced model is defined correctly, the problem was that I call a
namespace within a namespace.
I could solve this by calling my models like ::Webshop::Category instead
of Webshop.Category.

Anyway, it looks like everybody is against namespacing your models, but
I thought it was a great solution classify everything nicely.
What are your thoughts about this?

-Danny

Hello Danny,

just a word of caution – expect serious problems with namespaced
models when using things like has_many :through. You have to declare
almost everything in associations (eg. Product
belongs_to :category, :class => ‘Webshop::Category’, foreign_key =>
‘category_id’). You encounter all sorts of problems and often the
solution you dig out from Google or forums is “don’t use namespaced
models” :slight_smile:

I recently reworked part of application I am working on and completely
kicked namespaced models out. It is really hard when the Magic stops
working and you have to just declare and declare and fix everything :slight_smile:

I’d be very interested in other people’s experiences with namespaced
models. (I personally think it’d be great if Rails would support
namespaced models. It really pain to not be able to group models
semantically this way.)

Cheers,

Karel


www.karmi.cz

Danny,

Webshop::Category.find is the correct way to call your namespaced
Rails models. I’m not sure exactly what your problem is, but there are
a few things to check.

  1. Make sure the namespaced model is defined correctly, it should be:

class Webshop::Category < ActiveRecord::Base
self.table_name = ‘webshop_categories’
end

I’ve found that you have to explicitly give it the table name.

  1. Make sure all of your model’s files are in a folder with with your
    namespace name. For example:

/models/webshop/categories.rb

Check those things and let me know if you are still having problems.

-Jake

On May 4, 3:04 am, Danny H. [email protected]

Thanks Jake,

The namespaced model is defined correctly, the problem was that I call a
namespace within a namespace.
I could solve this by calling my models like ::Webshop::Category instead
of Webshop.Category.

I see, I put my models and controllers in the same module and avoided
that issue. If you don’t really need an admin module you can just put
your controllers in the Webshop module and add the /admin/ part of the
url in your routes file.

Anyway, it looks like everybody is against namespacing your models, but
I thought it was a great solution classify everything nicely.
What are your thoughts about this?

There are times when it really makes sense to have some models in a
namespace and it is unfortunate that rails doesn’t support this very
well. As another person said, you loose a lot of the magic of rails.
Another option is to just name your models something like
WebshopCategory, which avoids name conflicts, but just feels wrong to
me. I chose to use real namespaces because it seemed to make sense for
my particular project. I haven’t run into any problems as Karel seems
to have had, but you do have to declare everything, which is a pain.

Ryan B. wrote:

Please! Don’t namespace your models as they don’t need to be namespaced!
I don’t care what script/generate scaffold does, it’s WRONG.

Why? What makes it wrong? I’ve never attempted to put a Rails model
inside a module, but I’m curious as to your reasoning.

The only reason is that Rails has problems with namespaced models. The
problem is with Rails, not namespace modules. I just saw that Josh
Susser has a post that outlines some of the problems namespaces cause
in Rails:

http://blog.hasmanythrough.com/2008/5/6/a-simple-alternative-to-namespaced-models

Unfortunately, his solution doesn’t help with name conflicts, which is
what I need namespaces for.

-Jake

Ryan B. wrote:

Please! Don’t namespace your models as they don’t need to be namespaced!
I don’t care what script/generate scaffold does, it’s WRONG.

Why? What makes it wrong? I’ve never attempted to put a Rails model
inside a module, but I’m curious as to your reasoning.

Is broking for me too, only in edge.
It

http://blog.hasmanythrough.com/2008/5/6/a-simple-alternative-to-namespaced-models
Don’t work for me.

Suggestions?

On Tue, May 6, 2008 at 12:56 PM, Jake J. [email protected]
wrote:

problem is with Rails, not namespace modules. I just saw that Josh


Kivanio Pereira Barbosa
Cel 8121-4248

www.eiqconsultoria.com.br

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs