Today I Learned

hashrocket A Hashrocket project

Generate Rails Models with Namespaces

You can use the rails model generator to generate namespaced models. I always have a hard time remembering the syntax for namespaces, but it's pretty straightforward.

Say I want to create a model Blog::Post. The generator for this is:

rails generate model blog/post title:string ...

This will generate the following files (assuming you're using rspec and FactoryBot)

invoke  active_record
create    db/migrate/20241028193321_create_blog_posts.rb
create    app/models/blog/post.rb
create    app/models/blog.rb
invoke    rspec
create      spec/models/blog/post_spec.rb
invoke      factory_bot
create        spec/factories/blog/posts.rb

If the namespace already exists, you'll be prompted to either overwrite or keep the existing app/models/blog.rb file (you'll probably want to keep the existing one).

Happy generating!

See More #rails TILs
Looking for help? Hashrocket has been an industry leader in Ruby on Rails since 2008. Rails is a core skill for each developer at Hashrocket, and we'd love to take a look at your project. Contact us and find out how we can help you.