DEV Community

Cover image for Typical folder structure for a Ruby application using Sinatra and ActiveRecord
Jaime Montes de Oca
Jaime Montes de Oca

Posted on

Typical folder structure for a Ruby application using Sinatra and ActiveRecord

When writing a software application, separation of concerns is very important, especially with object oriented programming. It is key that we use a folder structure that is easy to work with and clearly let us know where to put each file.


This is a suggestion on how to make this folder structure when you are coding in Ruby using Sinatra and Active Record. Each folder in this structure has a clearly defined responsibility as well as each file inside of them.

Files in the root folder:
Gemfile: Lists all the gems that need to be installed for our application to run.
Rakefile: Contains code we create and use for common tasks we can run from the command line and also by requiring sinatra/activerecord/rake we can have access to other useful tasks like db:migrate or db:seed.

Folders:
app/models: Here we put all our Active Record models, the core of our application because here we access and update the database responding to the user actions.
app/controllers: Following the software pattern known as Model-View-Controller, this folder contains the code for creating our API end points that are responsible for feeding the client app.
config: Code responsible for our environment setup. Here we define how to connect to the database and require files or gems needed by our application.
db: Has the different databases we are using for each environment (development/test).
db/migrate: Files to create/alter the database structure.
db/seed.rb: File that contains code to add sample data to our database. Here is a useful gem for generating sample data. We can use the rake task db:seed to add that sample data to the database.
spec: Contains all the test files.

Following a well designed folder structure will make it easy to know where to put code depending of its responsibility, will help our application to grow under control and will help other developers to easily maintain the application in the future.

Top comments (0)