Tuesday, September 20, 2011

change table column name in Rails validation message

class Vendor < ActiveRecord::Base
validate_uniqness_of :gid, :message => "should be uniq"
    :gid => "vendor"

  def self.human_attribute_name(attr)
    HUMANIZED_ATTRIBUTES[attr.to_sym] || super


Friday, July 8, 2011

Collect all child elements javascript

For all first level children
var children = document.getElementById('id').childNodes;

For all descendants 
var children = document.getElementById('id').getElementsByTagName('*');

For some particular  element children
var childNodeArray = document.getElementById(tbody).getElementsByTagName('tr');

Monday, March 28, 2011

find process id for the port

Find port by following way

lsof -w -n -i tcp:8080
fuser -n tcp 8080

Now you have some integer value which is the process of the port
Now kill it by
kill -9 XXXX

Saturday, March 26, 2011

Debuging Technique with Ruby on Rails

There are multiple debug techniques are available with RoR,  Please use the
below techniques as much as you can,  while you do programming in RoR.

1. install ruby-debug
   then need to start application by ruby script/server --debugger
   And place word 'debugger' anywhere in your code and once you run your
application, it stops running from where you have placed 'debugger'. We
can say that debugger is the breakpoint

  Then you can use following commands
  n - move next line
  l - display the current line after the debugger
  c - continue
  You can also check the readme of the ruby-debug

2. You can use puts,exit etc.

3. You can use  logger.debug "The object is #{@object}" or  
  RAILS_DEFAULT_LOGGER.debug @object

4. In view, you can also use <%=debug @object %>

5. You can simultaneously check your log by running tail -f lod/development.log in the another terminal or you can also use tail -n log/development.log where n is any numeric number

6. We can use Exception Notifier plugin in our production environment so that we gets an notification mail if any error occurs on production

Monday, January 24, 2011

Call Rake task from migration

 You can call the rake task in migration by writing following


Is the string has numeric value?

for check the string has numeric value or not then you can do it by below
s = "123cs"

then call method as below

and the method is as below
 def is_numeric?(s)
    s.to_s.match(/\A[+-]?\d+?(\.\d+)?\Z/) == nil ? false : true

Run your rails application with apache2 and mongrel_clusters

Run your rails application with apache2 and mongrel_clusters

Install Apache 2.2 and enable the needed modules (url rewriting, proxy, proxy_balancer e proxy_http)

sudo apt-get install apache2

# enable your modules in apache
sudo a2enmod rewrite
sudo a2enmod proxy
sudo a2enmod proxy_balancer
sudo a2enmod proxy_http
sudo a2enmod proxy_connect

# if you not enabled any of the modules then may be errors occured while you restart your apache after adding the below configration
# Error may be encoutered it yoou have not enabled module rewrite is "Invalid command 'RewriteEngine', perhaps misspelled or defined by a module not included in the server configuration"

Install Mongrel, create a mongrel user, create the mongrel cluster
sudo gem install daemons gem_plugin mongrel mongrel_cluster --include-dependencies

You can continue with the same linux user to mongrel if you want otherwise you can create new user by
sudo /usr/sbin/adduser mongrel
This line creates new user mongrel withg same mongrel group
Now need to create one mongrel_cluster.yml
sudo mongrel_rails cluster::configure -e production -p 3010 -N 2 -c /home/user/projects/myapp -a --user mongrel --group mongrel

here -e = your working environment
     -p = your port for run application
     -N = Number of instances of the application. here 2 instances for my application and port is 3010. so this application will work on 3010 and 3011 port
 --user = need to provide your linuxuser
--group = need to provide your linuxgroup

this line will create /config/mongrel_cluster.yml file with following command
user: mongrel
cwd: /home/user/projects/myapp
log_file: log/mongrel.log
port: "3010"
environment: production
group: mongrel
pid_file: tmp/pids/mongrel.pid
servers: 2

now make sure that tmp and log folder has permission to the user/group which you have user in mongrel_cluster if not assign permission by
sudo chown -R mongrel:mongrel /path/to/app/tmp

# Now start mongrel clusters
sudo mongrel_rails cluster::start

Hopefully clusters is worked perfactly and you can get it run on multiple port 3010 and 3011

Now need to set proxy balancer for this app
For this need to create one host for you app
Go to sudo nano /etc/hosts localhost myapp

Now need to configure Apache configuration file (sudo gedit /etc/apache2/sites-available/default).

NameVirtualHost *:80

#we need this as on Ubuntu/debian by default Proxy is not allowed
<Proxy *>
  Order allow,deny
  Allow from all

#Proxy balancer section (create one for each ruby app cluster)
  <Proxy balancer://myapp_cluster>
    BalancerMember http://myapp:3010
    BalancerMember http://myapp:3011

#Virtual host section (create one for each ruby app you need to publish)
<VirtualHost *:80>
    ServerAdmin webmaster@localhost
        ServerName myapp  
        DocumentRoot /home/user/projects/myapp/public/
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    <Directory /home/debian/chirag/mycalltime/public/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all

    ErrorLog /var/log/apache2/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog /var/log/apache2/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from ::1/128

  #Rewrite stuff
  RewriteEngine On

  # Check for maintenance file and redirect all requests
  RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f
  RewriteCond %{SCRIPT_FILENAME} !maintenance.html
  RewriteRule ^.*$ /system/maintenance.html [L]

  # Rewrite index to check for static
  RewriteRule ^/$ /index.html [QSA]

  # Rewrite to check for Rails cached page
  RewriteRule ^([^.]+)$ $1.html [QSA]

  # Redirect all non-static requests to cluster
  RewriteRule ^/(.*)$ balancer://myapp_cluster%{REQUEST_URI} [P,QSA,L]


After configure the default file need to restart apache
sudo /etc/init.d/apache2 restart

You get your app running on multiple instanses with proxy balancer

Contact Me for any help regarding rails/nodejs/php/mysql


Email *

Message *