Reputation: 65093
So, my list of models in app/models is getting kinda long, and I'd like to organize it.
but, I'm aware of Ruby/Rails' naming convention for folders / sub classes.
i.e.
Object::MyObject::SubObject
would look like this:
object.rb
object/
-- my_object.rb
-- my_object/
-- -- sub_object.rb
at least to my understanding
but, what I want to do is group classes that are related to each other or interact a lot.
Here is what I would like to do (as an example, these aren't my actual classes):
app/models/
-- library_item.rb # LibraryItem < ActiveRecord::Base
-- library_items/
-- -- book.rb # Book < LibraryItem
-- -- book/
-- -- -- page.rb # Page < ActiveRecord::Base
-- -- -- cover.rb # Cover < ActiveRecord::Base
-- -- magazine.rb # Magazine < LibraryItem
-- -- magazine/
-- -- -- shiny_page.rb # ShinyPage < ActiveRecord::Base
is this possible? I know it is with Java, but Ruby and Rails have a very specific way of doing things, and I haven't ever read anything much about organization of large projects.
Upvotes: 0
Views: 123
Reputation: 27779
For Rails to automatically find these models, the class names need to match the file path, e.g.:
class LibraryItem::Book::Page < ActiveRecord::Base
Personally I prefer to keep my model hierarchy mostly flat so I that I can just type Page
instead of LibraryItem::Book::Page
. You can accomplish both of these things -- a file hierarchy plus short model names -- by explicitly loading your models, perhaps in an initializer.
Upvotes: 3