Deployment of Rails App on DigitalOcean with Capistrano, Nginx and Passenger - Part 2

In the previous article, we have seen how to configure DigitalOcean Droplet. Now let's proceed further to set up our application repository and Ngnix with Phusion Passenger on the DigitalOcean droplet. 1. Install RVM and Ruby We will install ruby using Ruby Version Manager(RVM) as it gives more flexibility to manage multiple ruby versions on the same system over rbenv, chruby, etc. Import RVM GPG key, before installing RVM: gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 Then, install RVM: curl -sSL https://get.rvm.io | bash -s stable Load RVM script and run requirement command to manage the

Deployment of Rails App on DigitalOcean with Capistrano, Nginx and Passenger - Part 1

Nginx is a lightweight, high-performance web server. Passenger used with Nginx gives us many advantages. It automatically serves static assets from the public subdirectory to improve performance. Capistrano is used to automate deployment process to your remote server. Using Capistrano, you can deploy your web application on many servers simultaneously in sequence or as a rolling set. In this article, we will see basic steps that need to be followed to configure your DigitalOcean Ubuntu Droplet. Prerequisites: Ubuntu 16.04 x64 Droplet Working Rails app hosted in a remote git repository that's ready to be deployed Initial setup for the

Debugging tips and tricks in Ruby and ROR - Part 1

In software development you are destined to deal with lots of code that is not written by you. What would be more stressful than debugging the code that you barely understand. You might have been in the situation where you have been assigned a bug and you have to traverse through lots of file which you have no idea of, just get to root cause of the issue. Being in these situations many times, I have learned few tips and tricks for debugging and would like to share it here. **Checking where the method causing a problem exists.** In some

Distributed indexing and searching with Apache SolrCloud

Since quite some time, we use Apache Solr in one of our project for indexing data to search it faster from Solr server instead of always searching it from main database and creating bottleneck. We use Sunspot gem which is ruby library for rails application to implement Apache solr. Recently we thought to implement Apache SolrCloud architecture in same application to manage our daily growing Solr data more efficiently with this powerful feature of Apache Solr. In brief, you can consider it as a another database where your data is indexed and stored as xml documents. You can query this

Understanding PG Result object

In rails one of the way to execute raw query is use of ActiveRecord::Base.connection.execute We are going to see if we use this option to execute raw query and we are working with pg database then what result it provides and what operations can be performed on that result. results = ActiveRecord::Base.connection.execute('select * from purposes') It returns PG::Result object : #<PG::Result:0x007fd5a09686d8 status=PGRES_TUPLES_OK ntuples=3 nfields=5 cmd_tuples=3> We can verify it simply using .class as we normally do in ruby. results.class => PG::Result This

Performance Optimisations on a Rails App

Method Missing Method Missing is one of the concepts of metaprogramming ruby. Although metaprogramming is very powerful it too has some shortcomings especially speed. A normal method is comparatively 1.5x times faster than a missing method. One of our users complained about our calendar page being very slow - on inspecting we found request was taking more than 30 seconds for loading the month view of the calendar and eventually request timed out. The user had lots of activities on that calendar - 2000+. On further inspection, we found our DB query was quite fast but each activity was

Password protect PDFs

In one of our Rails application, we needed to generate password protected PDFs. We used wicked_pdf (an awesome gem that generates PDFs from HTML template) to generate PDFs but it doesn't provide feature to secure it. While searching for solution to secure PDFs we came across PDFtk (PDF toolkit). It is a cross-platform tool for manipulating PDF documents. It has feature to add password to PDF document using "user password" as well as "master password" (owner password) which is great. Installing PDFtk You need to install "PDFtk Server" which is a command-line tool

Adding new column with default value to high volume database table

Almost all Ruby on Rails developers might come across scenario where they need to add a new column with a default value to one of the database tables. Most of us (including me) would write following migration statement - add_column :table_name, :column_name, :boolean, default: false This is a good practice but would cause downtime if the table has large number of records. It took 3 secs when I ran the migration for a table having 50k records. -- add_column(:table_name, :column_name, :boolean, {:default=>false}) -> 3.3695s 3 secs is a long

Referencing Rails Assets in Javascript / CoffeeScript files

Sometimes you’ll want to refer to your image assets from inside of your JavaScript or CoffeeScript files. We have nice rails helpers that would allow us to do so but we need to append .erb to every .js or .coffee file we want to reference images. I didn’t like it that way, because ERB inside of CoffeeScript looks odd and having the file end with .erb messes up syntax highlighting. A way around this is by adding following piece of code (not the beautiful one) to one single file that ends on .erb (i.e. js_assets.js.

Communicating with Sendgrid V3 API

Sendgrid provides email service which we can use for sending emails from our app and we can even track every details of any email sent through our Sendgrid account like whether it is delivered, opened, dropped and details of unsubscribed email addresses etc. There are also API endpoints provided by Sendgrid to access user subscribe/unsubscribe related data and email event data with respect to emails sent through your sendgrid account for which documentation link is provided at bottom. To understand this lets go through simple example to fetch sendgrid subscription groups data from your sendgrid account - Generate API