Namespaced models
Published over 5 years ago
I don’t really understand why people use namespaced models. I see ActiveRecord models as DSL for database. There is no concept of namespacing in Database, then why should you have them with models ? Apart from that, they are very buggy too !
“I am generally not a huge fan of namespaces for models. As I don’t think that’s a good fit for splitting up your domain.”
- DHH
From what I’ve seen, the most common explanations given are :
Now let’s look at elegant solutions for both these problems.
For the purpose of this article, let’s assume you have models for different kind of pets. e.g. Dog, Cat & Rabbit.
Rails by default, wants you to put all your models in RAILS_ROOT/app/models directory. But that’s a convention. There is absolutely nothing that stops you from putting your model files anywhere you wish and organize them according to your liking and based on application specific logical groups.
Rails::Initializer.run do |config| # Your existing stuff config.load_paths << "#{RAILS_ROOT}/app/models/pets" end
That’s it ! Now you can have dog.rb, cat.rb & rabbit.rb inside RAILS_ROOT/app/models/pets directory.
Two ways to skin this cat :
Abstract models are the models which cannot have objects ( cannot be instantiated ) and hence they don’t have associated table as well. Every rails developer uses abstract model in their code without knowing it. ActiveRecord::Base. In our case, we can have an abstract model called Pet for keeping the common behavior of all the pets. And our models would look something like :
class Pet < ActiveRecord::Base self.abstract_class = true
class Pet < ActiveRecord::Base
self.abstract_class = true belongs_to :person validates_presence_of :nameend
- RAILS_ROOT/app/models/pets/dog.rb
class Dog < Pet def bark "baaw" end endThat’s it. Dog will inherit all the methods/validations/associations from parent Pet model and so will all the other models who would inherit from Pet abstract model. Please note that this is not STI as we have set self.abstract_class = true in Pet.