Friday, December 11, 2015

Rails Pluck

  • if you only need the id with Rails 4, use ids: User.first.gifts.ids
  • if you only need some fields with Rails 4, use pluck: User.first.gifts.pluck(:id, :name, ...)
  • if you only need one field with Rails 3, use pluck: User.first.gifts.pluck(:id)
  • if you need all fields, use collect
  • if you need some fields with Rails 4, still use pluck
  • if you need some fields with Rails 3, use selectand collect

Thursday, February 13, 2014

The Professional Programmer

The single most important trait of a professional programmer is personal responsibility. Professional programmers take responsibility for their career, their estimates, their schedule commitments, their mistakes, and their workmanship. A professional programmer does not pass that responsibility off on others.
If you are a professional, then you are responsible for your own career. You are responsible for reading and learning. You are responsible for staying up to date with the industry and the technology. Too many programmers feel that it is their employer’s job to train them. Sorry, this is just dead wrong. Do you think doctors behave that way? Do you think lawyers behave that way? No, they train themselves on their own time, and their own nickel. They spend much of their off-hours reading journals and decisions. They keep themselves up to date. And so must we. The relationship between you and your employer is spelled out nicely in your employment contract. In short: your employer promises to pay you, and you promise to do a good job.
Professionals take responsibility for the code they write. They do not release code unless they know it works. Think about that for a minute. How can you possibly consider yourself a professional if you are willing to release code that you are not sure of? Professional programmers expect QA to find nothing because they don’t release their code until they’ve thoroughly tested it. Of course, QA will find some problems, because no one is perfect. But as professionals, our attitude must be that we will leave nothing for QA to find.
Professionals are team players. They take responsibility for the output of the whole team, not just their own work. They help one another, teach one another, learn from one another, and even cover for one another when necessary. When one teammate falls down, the others step in, knowing that one day they’ll be the ones to need cover.
Professionals do not tolerate big bug lists. A huge bug list is sloppy. Systems with thousands of issues in the issue-tracking database are tragedies of carelessness. Indeed, in most projects, the very need for an issue-tracking system is a symptom of carelessness. Only the very biggest systems should have bug lists so long that automation is required to manage them.
Professionals do not make a mess. They take pride in their workmanship. They keep their code clean, well structured, and easy to read. They follow agreed-upon standards and best practices. They never, ever rush. Imagine that you are having an out-of-body experience watching a doctor perform open-heart surgery on you. This doctor has a deadline (in the literal sense). He must finish before the heart-lung bypass machine damages too many of your blood cells. How do you want him to behave? Do you want him to behave like the typical software developer, rushing and making a mess? Do you want him to say, “I’ll go back and fix this later”? Or do you want him to hold carefully to his disciplines, taking his time, confident that his approach is the best approach he can reasonably take. Do you want a mess, or professionalism?
Professionals are responsible. They take responsibility for their own careers. They take responsibility for making sure their code works properly. They take responsibility for the quality of their workmanship. They do not abandon their principles when deadlines loom. Indeed, when the pressure mounts, professionals hold ever tighter to the disciplines they know are right.
—Robert C. Martin (Uncle Bob)

Tuesday, December 10, 2013

Setting up Unicorn + Ngnix in Ubuntu for Ruby on Rails

To get you started, Let’s install Nginx first by using apt-get package manager.
$ sudo apt-get install nginx
Sometime you get ERROR because your machine packages are not upto date. Run apt-get update to downloads the package lists from the repositories and “updates” them to get information on the newest versions of packages and their dependencies. It will do this for all repositories and PPAs.
$ sudo apt-get update
Once your nignx will install, you command window will prompt:
Setting up nginx-full (1.2.1-2.2ubuntu0.1) ...
Setting up nginx (1.2.1-2.2ubuntu0.1) ...
Processing triggers for libc-bin ...
ldconfig deferred processing now taking place
Processing triggers for sgml-base ...
Updating the super catalog...

At this point switch to /etc/nginx/ and /etc/nginx/sites-available configuration directory and have a look to nginx.conf and default respective configuration file. It’s self explanatory under commented in-lines. If still you need more details. I will recommend you to check wiki.nginx for more info.
While the server configuration it’s always better to create separate user(which we have more control over than nobody) for security reasons and increased control by using below command:
$ sudo useradd -s /sbin/nologin -r nginx
$ sudo usermod -a -G web nginx
Starting/stopping/restarting Nginx is pretty straight forward.
$ sudo service nginx start/restart/stop
Now let’s install Unicorn, Unicorn is distributed as ruby gem So you can install vi ruby gem.
$ gem install unicorn
Once Unicorn installed successfully. We need to create two configuration file.
1.Replace /etc/nginx/sites-available/ default file with below code:
upstream shop {
server unix:/tmp/unicorn.shop.sock fail_timeout=0;
}

server {
listen 80;# default deferred;
server_name localhost;
root /var/www/shop/public;
try_files $uri/index.html $uri @shop;
location @shop {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://shop;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}

2. Create a unicorn.config file in your Rails application configuration file with below code:
working_directory "/var/www/shop"
pid "/var/www/shop/tmp/pids/unicorn.pid"
stderr_path "/var/www/shop/log/unicorn.log"
stdout_path "/var/www/shop/log/unicorn.log"

listen "/tmp/unicorn.shop.sock"
worker_processes 2
timeout 30

You have to tweak a few things to set the right paths in above configuration. Change “/var/www/shop” with your rails application directory.
In my case my Rails application is in /var/www/ with shop name.
Before start your rails application, make sure to include unicorn gem in your Gemfile and run bundle to install all dependencies.