Send STDERR to a separate file in Rails production

By default, Rails writes both STDOUT and STDERR streams to “<environment>.log” file. Fox example, in production, the file production.log has the request log as well as any application errors. But it is helpful, if you can send the errors to a separate file, that you can watch for errors.

Though there are gems to do this, the easiest way is to use the broadcast method available in Rails logger. Create a file with the following content and place it in ‘initializers’ directory.

That should set it up.

Prevent ViewPager from taking the full screen

The ViewPager does not support wrap_content for layout_height attribute as it never have all its children loaded at the same time, and can therefore not get an appropriate size (the option would be to have a pager that changes size every time you have switched page).


for your viewpager element. You will see both ViewPager and your second layout (concluded_matches_layout, in this case) in the same screen as below:

ViewPager and Another Layout in Same Screen

References – and

Issues while migrating MySQL to Postgres database

Like everyone else, I started to see the disadvantages in MySQL. Lack of ACID support, no analytical functions, no strict ANSI SQL compliance, no full text search etc.,

Following are the things to take care when you have plans to migrate your data to Postgres from MySQL

  1. Column names with mixed lower and upper cases
  2. Limit Clause SYNTAX change
  3. DATE_FORMAT function
  4. Convert TINYINT(1) to BOOLEAN datatypes
  5. Division by 0
  6.  null value in column “<column_name>” violates not-null constraint
  7. PG::CharacterNotInRepertoire: ERROR:  invalid byte sequence for encoding “UTF8″: 0xe92042 (Sequel::DatabaseError)
  8. Escape single quotes – :%s/\\/’/g

Ignore/Disable Crashlytics reports during App Development

In your ApplicationController (or the class that extends Application),

public void onCreate() {
Crashlytics crashlytics = new Crashlytics.Builder().disabled(BuildConfig.DEBUG).build();
Fabric.with(this, crashlytics);

Gradle will generate the appropriate value for BuildConfig.DEBUG based on your current build.

Editing /etc/hosts file in Genymotion Emulator

I use my local Rails app while developing the Android app. When I tried to connect “http://localhost:3000″ using Volley from the Android app that runs under Genymotion, it threw timeout error. It is because Genymotion runs under Virtualbox and Mac is it’s host OS. To get the IP of your host machine:

☁ duggout [master] ⚡ ifconfig vboxnet0
vboxnet0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500
ether 0a:00:27:00:00:00
inet netmask 0xffffff00 broadcast

Now you can use “″ in your Android app to connect to the localhost of your host OS.

If you need to work with subdomains (mostly required for API driven applications) where your application requires the URL to be of the form “”, you need to edit the hosts file of the virtualbox OS.

Connect your Genymotion device with adb.

./adb devices

to see your Genymotion device. If you do not see your device, open Genymotion settings, goto ADB tab, choose your ADB location instead of the default ADB provided by Genymotion. Restart emulator.

Create a new file (/tmp/hosts) in your system with the following contents: localhost

Now you need to push this file to the Genymotion Virtualbox OS using adb.

./adb root

./adb remount

./adb push /tmp/hosts /system/etc/

Restart emulator. Now you can connect using both localhost and from your Android code.

Include JST EJS Partials in Rails Backbone

Template File

Partial file

Final Output when you render the template:

Refer this issue in GitHub.

Verify Discourse with Google Webmaster with HTML File Upload method

My DuggOut Forum is hosted on a subdomain. Though I have verified the parent domain ( with Google Webmaster already, it still asked me to verify this subdomain.

Since all the other methods (Google Analytics, CNAME, TXT record, meta tag) failed, I had to go with HTML file upload based verification.

1. SSH into your host machine

2. SSH into the docker container

cd /var/discourse (or wherever discourse is installed)

./launcher ssh app

3. Create the Google provided HTML file under /var/www/discourse/public directory

Now, go and hit the URL –****.html

You will see it working and your Webmaster verification is complete.

Advanced queries using ActiveRecord in Rails

1. Replace IN Clause with Sub Query

While writing a strategy for my Cricket Stats Search engine, I had a scenario like this:

I need to generate the list of match innings that belongs to a particular set of matches (say, matches starting on a particular date).


matches = Match.where(start_date: '2014-02-18')

Then I wanted to use this in an IN clause while fetching matches innings. So I went ahead and wrote a ActiveRecord query like this:

MatchInning.where(match_id: matches.pluck(&:id))

This would generate the following SQL:

SELECT * FROM matches_innings WHERE match_id IN (1,2,3,4,5,6....10)

This query is highly inefficient as this runs 1 query to fetch matches and then another one with the list of values in IN clause. The SQL looks bad, and won’t work the number of values in matches is huge.

To fix this, I changed the query like this:

MatchInning.where(match: matches)

This would be the SQL:

SELECT * FROM matches_innings WHERE match_id IN (SELECT id FROM matches WHERE start_date = '2014-02-18')

For this to work, you should have set up the associations correctly. MatchInning should belongs_to match

PostgreSQL – Copy To and Copy From using Select queries

When you need to copy a small set of data from one database to an another (say, from dev system to a QA system), you could use the copy feature of PSQL.

To get the data in CSV of a select query:

duggout=> copy (select * from lookups where lkp_type = 'homepage_items' ) to STDOUT with csv;
6514,homepage_items,side_1,6,,,,,,,,,2014-10-06 15:47:25.216133,2014-10-06 15:47:25.216133
6515,homepage_items,list_main,"1,2,3,4,5,6,7",,,,,,,,,2014-10-08 02:52:52.995771,2014-10-08 02:52:52.995771
6516,homepage_items,image_main,"5,6,7",international_match,f,,,,,,,2014-10-05 14:06:55.591311,2014-10-11 08:14:47.196494

To load the data into the table in another environment:

COPY lookups FROM STDIN with csv;

Now, paste the CSV data with a trailing new line. And then \. (backslash with a dot). This should append the CSV data in the target table. When you want to copy more data, use the file option instead of STDIN/STDOUT. Check the doc for the usages.

To copy a large table, use the binary option:

remote_db=# copy table_name to '/tmp/a_path' with binary;
COPY 38765

You could copy the file to your local using scp, if the file is in a remote server. Then,

local_db=# copy table_name from 'local_path' with binary;
COPY 38765

It is as simple as that!