Its all about Ruby On Rails
Rails presentation and workshop at DayalBagh Educational Institute, Agra, India
Apr 4th
Two weeks ago we went to DEI(DayalBagh Educational Institute) to present Ruby and Rails. We also had a whole day workshop there.
It was really a big fun. We (me and my colleagues Sur & Hemant ) enjoyed it very much. People (students of M.Sc, PGDCSA and course coordinator) were very interested in Ruby and Rails. These students had already worked on PHP/Perl. First day sur presented Ruby basics then I gave a brief introduction of Rails and MVC Architecture. This was my first ever public presentation.
I was feeling very nervous while starting but later I felt comfortable. Afterward we had ActiveRecord Basics by sur, me on ActionController and ActionView by Hemant. Next day in workshop we planned to get tada list done by students but due to some time constraints we could not finish tada list completely . Students were confused in starting but later on they got interest in the application. After the workshop they were amazed how easily things can be done as comparing to PHP/Perl. I should say thanks to Ritu for taking initiative for this presentation and workshop. She was also supposed to go there but she couldn’t go.
Engine Yard: Best Hosting for rails applications
Mar 20th
Today I deployed my rails application to Engine Yard server. They have great configuration, ultimate cluster architecture(physical clusters then logical clusters). They have separate servers for SSH, email, web, app etc… . Servers are managed by top architects, I should say they are expert in rails application deployment.
ActiveRecord finder: Now we can use hash as conditions
Feb 22nd
Earlier conditions can either be specified as a string or array now in edge rails we can specify hash also. So if we specify conditions as hash then it will generate conditions based on equality with SQL AND.
Example:
User.find(:all, :conditions=>{:first_name => ‘akhil’, :role => ‘admin’})
will generate SQL as “where `first_name` = ‘akhil’ and `role` = ‘admin’”
Also we can have range as condition
Example:
User.find(:all, :conditions => {:access_level => 3..5})
will generate SQL as “where `access_level` BETWEEN 3 AND 5″
This is really helpful modification in ActiveRecord
Delegation in rails
Feb 19th
I don’t know why it is not mentioned any where in rails api, it could be very useful for all.
This is straight from rails\activesupport\lib\active_support\core_ext\module\deligation.rb :
Provides a delegate class method to easily expose contained objects’ methods
as your own. Pass one or more methods (specified as symbols or strings)
and the name of the target object as the final :to option (also a symbol
or string). At least one method and the :to option are required.
Delegation is particularly useful with Active Record associations:
class Greeter < ActiveRecord::Base
def hello()
“hello”
end
def goodbye()
“goodbye”
end
end
class Foo < ActiveRecord::Base
belongs_to :greeter
delegate :hello, :to => :greeter
end
Foo.new.hello # => “hello”
Foo.new.goodbye # => NoMethodError: undefined method `goodbye’ for #
Multiple delegates to the same target are allowed:
class Foo < ActiveRecord::Base
delegate :hello, :goodbye, :to => :greeter
end
Foo.new.goodbye # => “goodbye”
Ruby script for creating new rails project and initial SVN import (with ignoring/removing log/other files)
Feb 8th
Some days go I wrote a bash script for creating new rails project and initial SVN import (with ignoring/removing log/other files). And I received many comments if I could write a ruby script for this stuff. Hence here is the ruby script for creating new rails project and initial SVN import (with ignoring/removing log/other files). Copy this script to any of your directory as “create_rails_with_subversion.rb” and execute by issuing “ruby create_rails_with_subversion.rb “
You may need command line subversion for windows.
I have tested it at my windows and linux machine, if it not work for you please let me know.
Also I love to have your comments….
apache proxy balancer + mongrel clusters and deploying application with capistrano
Feb 4th
So you want to setup production server with mongrel clusters and apache proxy balancer, also wants to use capistrano for deployment, huh. Take it easy, its very simple.
You need Apache 2.2 or later on your production server, and the following ruby gems on your both machine(server and local):
- capistrano
- mongrel
- mongrel_cluster
I haven’t mentioned rails and rake gem as we are deploying a rails application so these gems are obvious.
Lets install above gems (if not installed) by issuing:
gem install –include-dependencies capistrano
gem install –include-dependencies mongrel
gem install –include-dependencies mongrel_cluster
Now make sure that the following modules are enabled (they are disabled by default) :
- mod-rewrite
- mod-proxy
- mod-proxy-http
- mod-proxy-balancer
to check if they are enabled issue ” /etc/init.d/apachectl -M ” on server, it will list all the enabled modules. For Debian systems all enabled modules are in /etc/apache2/mods-enabled directory and all available modules are in /etc/apache2/mods-available directory, to enable them issue ” a2enmod MOD_NAME “.
Now create the production database(on server) and update database.yml for production database settings.
After this configure mongrel by issuing ” mongrel_rails cluster::configure -e production -p 8000 -a 127.0.0.1 -N 2 -c ./ ” inside the rails application root directory(on client machine). This will create mongrel_cluster.yml in config directory. You can change parameters in this command, as -p 8000 specifies that mongrel instances will start up on port number starting 8000, -a 127.0.0.1 specifies that mongrel instances will listen to the localhost, -N specifies the number of mongrel instances and -c specifies the rails root directory.
Now its time to capistranize rails application, issue ” cap –apply-to ./ APP_NAME ” inside rails application root directory(on client machine), this will add two files(config/deploy.rb and lib/tasks/capistrano.rake) to the rails application. Edit deploy.rb according to your requirements. Also add the following code to deploy.rb :
task :restart, :roles => :app do
# stop mongrel clusters for previous release and start for current
run “cd #{previous_release} && mongrel_rails cluster::stop”
run “sleep 5″
run “cd #{current_release} && mongrel_rails cluster::start”
end
Now on your local machine issue these two commands in only once ” rake remote:setup ” and ” rake remote:cold_deploy “, when you issue ” rake remote:setup ” it will prompt for the server password and create necessary directories on the server and ” rake remote:cold_deploy ” will deploy your code to the server. Next time whenever you want to deploy to the server you just need to issue ” rake remote:deploy ” not ” rake remote:cold_deploy “.
Now we are just one step back, we need to configure apache proxy balancer for mongrel instances. Add the following code to the httpd.conf file:
BalancerMember http://127.0.0.1:8001
ServerName myapp.com
DocumentRoot /rails_apps/app/public
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f
RewriteCond %{SCRIPT_FILENAME} !maintenance.html
RewriteRule ^.*$ /system/maintenance.html [L]
RewriteRule ^/$ /index.html [QSA]
# Rewrite to check for Rails cached page
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/(.*)$ balancer://mongrel_cluster%{REQUEST_URI} [P,QSA,L]
you need to change the above code according to you requirement. Also you need to restart apache server by issuing ” /etc/init.d/apachectl restart “. Now you are done.
But we also need to add a script to start mongrel instances when the server restarts, otherwise whenever the server restart there will no mongrel instance running.
Just create a file named mongrel_clusters (you can choose any name) in /etc/init.d directory with the following code:
#!/bin/bash
#
# chkconfig: 345 94 16
# description: Startup script for mongrel
BASEDIR=/var/www/your_app
export HZ=100
export TERM=linux
export SHELL=/bin/bash
export HUSHLOGIN=FALSE
export USER=root
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games
export MAIL=/var/mail/root
export _=/usr/bin/env
export PWD=/etc/init.d
export HOME=/root
export SHLVL=2
export LOGNAME=root
cd $BASEDIR
case “$1″ in
start)
echo “starting up mongrel in $BASEDIR”
mongrel_rails cluster::start
;;
stop)
echo “stopping mongrel”
mongrel_rails cluster::stop
;;
restart)
mongrel_rails cluser::stop
sleep 3
mongrel_rails cluster::start
;;
esac
You need to change the BASEDIR in the above code. Make this file executable by ” chmod +x /etc/init.d/mongrel_clusters “
Issue these commands to add this script at system startup:
Debian: /usr/sbin/update-rc.d /etc/init.d/mongrel_clusters defaults
RedHat: /usr/sbin/chkconfig –add /etc/init.d/mongrel_clusters and /usr/sbin/chkconfig –level 2 /etc/init.d/mongrel_clusters on
Note: Use above instructions at your own risk, if your computer explodes its not my fault
Bash script for creating new rails project and initial SVN import (with ignoring/removing log/other files)
Jan 10th
Many times we need to create a new rails project and import it to SVN repository. Each time we have to repeat same commands for creating new rails project, Initial import to SVN, Ignoring *.log files, move database.yml to database.example, ignoring tmp/sessions etc.. etc..
So I have written a bash script that do all this stuff, all you need to do is copy this Bash script (for creating new rails project and initial SVN import with ignoring/removing log/other files) to any of your directory with name “create_rails_with_subversion” and execute this script by issuing ./create_rails_with_subversion & follow screen instructions.
This will:
- create a new rails project
- initial import to SVN
- remove and ignore all log files from svn
- remove and ignore sessions, cache sockets files from tmp dir
- move database.yml to database.example
- ignore database.yml
I also love to have your feedbacks…
UPDATE: I have modified this script, now it doesn’t remove tmp dir, it just ignore content in tmp/sessions, tmp/sockets and tmp/caches
UPDATE2: Script again modified , now it also freeze rails (you have to specify version).
What are your New Year’s Resolutions?
Jan 1st
Another year has come and gone, once again it’s time make a New Year’s resolution. Yeah!, I know many resolutions go unachieved and are often broken fairly shortly after they are set . But still I would like to set my new year’s resolutions . My resolutions are to improve my vocabulary and to think more realistic. What’s Yours ???
Also I wish 2007 will be a happy, peaceful, and prosperous new year for you all.
Plugin: Exception Notifier; Get detail information of exceptions occurred on the live server right on your inbox.
Dec 28th
I don’t know whether you guys are aware of Exception Notifier plugin written by Jamis Buck or not. If you don’t then I would like to share this with you.
- the sender address of the email
- the recipient addresses
- the text used to prefix the subject line
The email includes information about the current request, session, and environment, and also gives a backtrace of the exception.
I am using this plugin and found it very useful, and suggest you to use it.
You can get this plugin from http://dev.rubyonrails.org/svn/rails/plugins/exception_notification/
Most awaited moment…
Dec 17th
Tomorrow will be the our first working day in our new office at Gurgaon. We were planing to move since last year, but due to some reason we couldn’t. Now we are finally moving to our new office . Now we have enough space to hire people. If you are looking for a job/change or want to work on cutting edge web technology (Spec. Ruby on Rails), send me your resume at akhil at vinsol.com so that i can forward it for further considerations. If you don’t know ruby/rails no probs at all, Freshers OK.
Cheers…