Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should models be namespaced in Rails?

Usually there are a lot of models in a Ruby on Rails project, so:

  1. Is it a good practice to namespace them (in modules/folders)? What are the downsides? EG:

    • Shop
      • category.rb
      • details.rb
    • Products
      • category.rb
      • base.rb

    etc

    (instead of ShopCategory, to have Shop::Category?)

  2. Should also the controllers be namespaced in the same manner?

like image 419
Vlad Zloteanu Avatar asked Nov 22 '25 14:11

Vlad Zloteanu


1 Answers

I've recently found this post but back from 2007 by Pratik Naik. Says there namespace in models doesn't really resemble databases. Uses something like below. Even there's a quote from DHH too.

Rails::Initializer.run do |config|
  # Your existing stuff
  config.load_paths << "#{RAILS_ROOT}/app/models/pets"
end

http://m.onkey.org/2007/12/9/namespaced-models

p/s: I don't know whether the post is still relevant or not, just something I found recently when I wanted namespaces in my models.

like image 89
edthix Avatar answered Nov 24 '25 03:11

edthix