Ruby on rails Development with Activerecord

31 March 2016 by Gaurav Gaglani No comments

If you happen to work with Service Oriented Architectures. There might be many instances where you will end up creating multiple standalone applications. If you are in the Ruby ecosystem you will find that there is no standard structure your application. Like, for instance if you go about creating a standalone Java applications, the structure does appear like:

Most articles that you would come across suggest that, you could give your application some structure with a structure similar to gem. No better than the bundler gem to create your application. We followed the same approach but just added a some more change to the structure. If you are using RVM the first step would be to select the Ruby version and the appropriate gemset.

Next, we will need to install the bundler gem. To check the if bundler gem:

Now we are ready to create our project. This will create your project directory. Now Let’s take a look at the project directory structure that got generated.

Since we want to leverage the niceties of ActiveRecord in the project and add its as a dependency. We will use mysql as our backend. Under the project directory finds the standalone_app.gemspec file add the following dependencies.

It’s time to load the required files in the classpath lets require the gem files. We will require the files at two places. One in the Rakefile where the ActiveRecord rake tasks can run independently. Second in the standalone_app/lib/standalone_app.rb. In the Rakefile we will make the following changes:

You will need to add the following code in the rake file to use the rake db:... tasks in the project.

The code above will include ActiveRecord configurations first setting the ENV for the application, followed by configuring the path of the database configuration file, then pointing the migrations directory standalone_app/db/migrations directory and seeds standalone_app/db/seeds.rb, set the fixtures path, Rest of it is including the active_record rake tasks.

Next, we will get ActiveRecord connections in the application too.


Time to configure the logger. We will create a file called standalone_app/application.rb.

Structure of your project:
Since we will add models to application, we will add the standalone_app/db, add the standalone_app/db/migrate directory to add the migrations. We will also, add standalone_app/db/seeds.rb for the seed data. We will also, add a directory like, standalone_app/app/models to add your models. For the logger, we will create standalone_app/log and then add the standalone_app.log file.

Your business logic can reside in the lib/standalone_app/<files…>

When you are developing a production level application and with active_record. There are a couple nice to haves. One is access to the console like you get in rails.
Let’s create a bin directory
The first file we create is setup. Here are the contents of the file.

To get the console working, we will first add a standalone_app/bin directory to the project. Then inside it we will go about creating a file console.

To access the console
you will just: ./bin/console

When we want to run the application we can start and daemonize the application. Let’s go ahead and create a file: runner_cli

That’s it we are all set. You could find the source code of the project at: Github

Gaurav Gaglani

Follow me on Twitter

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe To Our Blog

Get access to proven marketing ideas, latest trends and best practices.

Next up home


Lets build cool stuff

Share your contact information & we will get in touch!

I want (Tell us more about your dream project)