Using UUID as Primary Key in Rails4 With Postgres

UUID. means Universally Unique Identifier.

The original intent while creating UUIDs was to create distributed systems to uniquely identify objects without significant central coordination. Saying which means, anyone can create UUIDs and use them to identify something. There is a very high probability the same key will never be unintentionally created by anyone else to identify some other kind of object. Objects labeled with UUIDs can therefore be later merge into a single database without needing to resolve key/identifier(ID) conflicts.

Rails 4 gives native support for the type UUID in Postgres. In the following post I will explain how we can enable and use the UUID your Rails code.

Read on →

Getting Started With Redis in Ruby

Redis is an open source, BSD licensed, advanced key-value store. It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets and sorted sets.

To get started first we need is gem 'redis'. Open up irb console. Now working with redis is as simple as :

require 'redis'

redis_connection = Redis.new(:timeout => 0)
=> #<Redis client v3.0.7 for redis://127.0.0.1:6379/0>

redis_connection.set("key", "value")
=> "OK"

redis_connection.get("key")
=> "value"

redis_connection.del("key")
=> 1

redis_connection.get("key")
=> nil # As we have delete the key value from redis memory.

By default our redis connection is connected to localhost, but we can specify the server ip where we want redis connection to be established.

redis_connection = Redis.new(:timeout => 0, :host => 123.45.67.89)

Make sure redis on 123.45.67.89 is running and accepting connections from outside , other than localhost.

For more detailed list of commands for redis check here.

Can’t Find ModelName With Id = 12345

Very often we use Callbacks to perform several actions after creation of a record. Also sometimes make a queue for SideKiq workers to work on. Let’s assume a model User.

user.rb
1
2
3
4
5
6
7
8
9
10
class User < ActiveRecord::Base

  # ===== Call Backs ==== #
  after_create :do_some_action

  def do_some_action
      UserSetupWorker.perform_async(self.id)
  end

end
Read on →

Set-up Guide for Rails Server on Ubuntu

Create a ubuntu 13.04 machine at desired hosting. I prefer using Digital Ocean for me.

After creating machine, login with root user.

ssh root@123.45.67.89

Create a user named deploy with sudo permission. We will use this to carry out all operation instead of using root user. Also choose a strong password for your user.

sudo useradd -s /bin/bash -m -d /home/deploy  -g root deploy
sudo passwd deploy
sudo usermod -a -G sudo deploy

Logout from your server and run the following command to copy your ssh keys.This will allow you to do a passwordless login via ssh.

ssh-copy-id deploy@123.45.67.89
ssh deploy@123.45.67.89
Read on →

Execute Sudo Commands Without Password Prompt

Executing sudo commands always require password after the command such as :

sudo service apache2 restart

To make it run we can add this to our whitelist where we dont have to input password everytime. Follow the following steps :

sudo touch /etc/sudoers.d/custom_permissions
echo "user_name ALL = (root) NOPASSWD: /etc/apache2" >> /etc/sudoers.d/custom_permissions
# you can use vi or some other editor also
sudo chown 0440 custom_permissions

NOPASSWD tells that password should not be prompted in case of this command execution. We can also add multiple commands using

user_name ALL = (root) NOPASSWD: /etc/apache2, /etc/nginx, /path/to/command, /path/to/another/command

Now all four commmands given above are executed without any password confirmation. We can mark all sudo commands by :

user_name ALL=(ALL) NOPASSWD: ALL

Be careful while doing this. Proceed with caution. Incase of some issue contact here.

Dancing Quick Sort

Watch on youtube

Getting Started With Javascript Plugins

One can get started with creating javascript plugins easily. A to do guide for creating a simple pliugin is given here.

reddish.js
1
2
3
$.fn.reddish = function() {
  this.css( "color", "red" );
};

This defines the plugin. It will add red colour to any element on which reddish method will be called.

Read on →

Checkout to a Branch on Local Machine Linked With Same Branch on Remote Origin

We can know all the existing branches of any project by git branch -a. It gives output something like

* master
some_feature_branch
remotes/origin/HEAD -> origin/master
remotes/origin/master-some_feature_branch
remotes/origin/master-some_another_feature_branch
  • `* master` tell the current branch.
  • Branch names starting from `remotes` tell us that branch is at remote server.
  • Branch name with `->` tells us to which remote branch local branch is mapped with.

Now to create a new local branch mapped with remote branc, assuming remotes/origin/master-some_another_feature_branch do

git checkout -b master-some_another_feature_branch origin/master-some_another_feature_branch

Now the new local branch is created, mapped with remote branch.

Check if a Port Is Open on a Remote Server

Simply do

nc -z -w 200 173.194.36.68 6379; echo $?

or

nc -z -w 200 codesapling.com 6379; echo $?

If it returns 0 that means port is open, if 1 port is closed.

This takes a little bit of time. Be patient :)

Pull and Push Code From Different Repositories

A git repository has a remote origin where the code is pushed regularly. By default we have one remote origin. But some cases may require different origins e.g. My blog has its remote origin set to heroku. Comamnd

git push origin master
git push heroku master

Because both are pointed to same origin as we can see from

git remote -v show
heroku  git@heroku.com:my_blog_repo.git (fetch)
heroku  git@heroku.com:my_blog_repo.git (push)
origin  git@heroku.com:my_blog_repo.git (fetch)
origin  git@heroku.com:my_blog_repo.git (push)

Now, I want to keep a backup of everything apart from hosting on heroku lets say on my bitbucket repository. Simply add another

git remote add origin2 git@bitbucket.org:grsahil20/my_blog_repo.git

resultant is

git remote -v show
heroku  git@heroku.com:my_blog_repo.git (fetch)
heroku  git@heroku.com:my_blog_repo.git (push)
origin  git@heroku.com:my_blog_repo.git (fetch)
origin  git@heroku.com:my_blog_repo.git (push)
origin2 git@bitbucket.org:grsahil20/my_blog_repo.git (fetch)
origin2 git@bitbucket.org:grsahil20/my_blog_repo.git (push)

Now, code can be pushed to git repository hosted at bitbucket via

git push origin2 master

Happy Gitting :)

git