Wednesday, 7 March 2012

Difference between Validations, Callbacks and Observers

Validations allow you to ensure that only valid data is stored in your database.
Example: validates_presence_of :user_name, :password
                 validates_numericality_of :value

We can write custom validation also as

def validate
  errors.add(:price, “should be a positive value”) if price.nil?|| price < 0.01
end

Callbacks and observers allow you to trigger logic before or after an alteration of an object’s state.

Callbacks are methods that get called at certain moments of an object’s life cycle. With callbacks it’s possible to write code that will run whenever an Active Record object is created, saved, updated, deleted, validated, or loaded from the database.

Callbacks are hooks into the life cycle of an Active Record object that allow you to trigger logic before or after an alteration of the object state. This can be used to make sure that associated and dependent objects are deleted when destroy is called (by overwriting before_destroy) or to massage attributes before they’re validated (by overwriting before_validation)

Observers are similar to callbacks, but with important differences. Whereas callbacks can pollute a model with code that isn’t directly related to its purpose, observers allow you to add the same functionality outside of a model. For example, it could be argued that a User model should not include code to send registration confirmation emails. Whenever you use callbacks with code that isn’t directly related to your model, you may want to consider creating an observer instead.

Difference between Application server and Web Server

apache, nginx, IIS are web servers
mongrel, webrick, phusion passenger are app servers

App server is something which works with particular programming language and parses and executes the code
since mongrel and webrick can only work with rails, so they are app servers

Web servers are servers which can take the request from the browser.
Web servers normally works on port 80 though we can change the port in configuration 
since mongrel and webrick can take that request directly, so they can be thought of as web servers but web servers do have a lot of other functionality like request pipeline, load balancing etc.
App servers lack these functionalities.

About Mongrel server:
mongrel work as web as well as app server if you are talking about dev environment
but in production, mongrel alone can not work it will be too slow
so we need a web server in front of mongrel

Thanks to Mr. Sumit  Garg & Mr. Sidhant for clarifying these points in details.
I would love to have others view also on this.

Update Rails 3.1 to Rails 3.2


In Gemfile, change following versions of assets and rails

gem 'rails', '3.2.0'

group :assets do
  gem 'sass-rails', "  ~> 3.2.3"
  gem 'coffee-rails', "~> 3.2.1"
  gem 'uglifier', '>= 1.0.3'
end

Run 'bundle update'

Monday, 5 March 2012

Installing Ubuntu inside Windows using VirtualBox


Installing Ubuntu inside Windows using VirtualBox

Introduction
VirtualBox allows you to run an entire operating system inside another operating system. Please be aware that you should have a minimum of 512 MB of RAM. 1 GB of RAM or more is recommended.

Installation Process
The first thing you have to do is obtain VirtualBox. Visit the VirtualBox website’s download page.

After you launch VirtualBox from the Windows Start menu, click on New to create a new virtual machine. When the New Virtual Machine Wizard appears, click Next.
You can call the machine whatever you want. If you’re installing Ubuntu, it makes sense to call it Ubuntu, I guess. You should also specify that the operating system is Linux.
VirtualBox will try to guess how much of your memory (or RAM) to allocate for the virtual machine. If you have 1 GB or less of RAM, I would advise you stick with the recommendation. If, however, you have over 1 GB, about a quarter your RAM or less should be fine. For example, if you have 2 GB of RAM, 512 MB is fine to allocate. If you have 4 GB of RAM, 1 GB is fine to allocate. If you have no idea what RAM is or how much of it you have, just go with the default.
Click Next.
If this is your first time using VirtualBox (which it probably is if you need a tutorial on how to use it), then you do want toCreate new hard disk and then click Next.
Click Next again.
Theoretically, a dynamically expanding virtual hard drive is best, because it’ll take up only what you actually use. I have come upon weird situations, though, when installing new software in a virtualized Ubuntu, in which the virtual hard drive just fills up instead of expanding. So I would actually recommend picking a Fixed-size storage.
Ubuntu’s default installation is less than 4 GB. If you plan on adding software or downloading large files in your virtualized UBuntu, you should tack on some buffer.
Click Finish and wait for the virtual hard drive to be created. This is actually just a very large file that lives inside of your Windows installation.
Click Finish
 
The next thing to do to make the (currently blank) virtual hard drive useful is to add the downloaded Ubuntu disk image (the .iso) boot on your virtual machine. Click on Settings and Storage. Then, under CD/DVD Device, next to Empty, you’ll see a little folder icon. Click that, and you can select the Ubuntu .iso you downloaded earlier.
Once you’ve selected it, click OK.
Then double-click your virtual machine to start it up.
Once it’s started up, just follow the regular installation procedure as if you were installing Ubuntu on a real hard drive (instead of a virtual one).

Oracle VirtualBox virtualize


Oracle VirtualBox

VirtualBox is a powerful x86 and AMD64/Intel64 virtualization product for enterprise as well as home use. Not only is VirtualBox an extremely feature rich, high performance product for enterprise customers, it is also the only professional solution that is freely available as Open Source Software under the terms of the GNU General Public License (GPL) version 2. See "About VirtualBox" for an introduction.
Presently, VirtualBox runs on Windows, Linux, Macintosh, and Solaris hosts and supports a large number of guest operating systems including but not limited to Windows (NT 4.0, 2000, XP, Server 2003, Vista, Windows 7), DOS/Windows 3.x, Linux (2.4 and 2.6), Solaris and OpenSolaris, OS/2, and OpenBSD.
VirtualBox is being actively developed with frequent releases and has an ever growing list of features, supported guest operating systems and platforms it runs on. VirtualBox is a community effort backed by a dedicated company: everyone is encouraged to contribute while Oracle ensures the product always meets professional quality criteria.
VirtualBox platform packages. The binaries are released under the terms of the GPL version 2.
For More


How to Install Mysql On Ubuntu Install MySQL Server 5 on Ubuntu


Installing MySQL 5 Server on Ubuntu is a quick and easy process. It almost feels like
it should be more difficult.
Open a terminal window, and use the following command:


sudo apt-get install mysql-server
If you are running PHP you will also need to install the php module for mysql 5:

sudo apt-get install php5-mysql
To create a new database, use the mysqladmin command:


mysqladmin create <databasename>
See, really easy!

How to Install Tomcat 6 On Ubuntu


Installing Tomcat 6 on Ubuntu

If you are running Ubuntu and want to use the Tomcat servlet container, you should
not use the version from the repositories as it just doesn’t work correctly. Instead you’ll
need to use the manual installation process that I’m outlining here.
Before you install Tomcat you’ll want to make sure that you’ve installed Java.
I would assume if you are trying to install Tomcat you’ve already installed java, but if you
aren’t sure you can check with the dpkg command like so:
dpkg –get-selections | grep sun-java
This should give you this output if you already installed java:
sun-java6-bin install
sun-java6-jdk install
sun-java6-jre install
If that command has no results, you’ll want to install the latest version with this command:
sudo apt-get install sun-java6-jdk
Installation
Now we’ll download and extract Tomcat from the apache site. You should check to
 make sure there’s not another version and adjust accordingly.
wget http://apache.hoxt.com/tomcat/tomcat-6/v6.0.14/bin/apache-tomcat-6.0.14.tar.gz
tar xvzf apache-tomcat-6.0.14.tar.gz
The best thing to do is move the tomcat folder to a permanent location.
I chose /usr/local/tomcat, but you could move it somewhere else if you wanted to.
sudo mv apache-tomcat-6.0.14 /usr/local/tomcat
Tomcat requires setting the JAVA_HOME variable. The best way to do this is to
set it in your .bashrc file. You could also edit your startup.sh file if you so chose.
The better method is editing your .bashrc file and adding the bolded line there.
You’ll have to logout of the shell for the change to take effect.
vi ~/.bashrc
Add the following line:
export JAVA_HOME=/usr/lib/jvm/java-6-sun
At this point you can start tomcat by just executing the startup.sh script in
the tomcat/bin folder.
Automatic Starting
To make tomcat automatically start when we boot up the computer, you can add
a script to make it auto-start and shutdown.
sudo vi /etc/init.d/tomcat
Now paste in the following:
# Tomcat auto-start
## description: Auto-starts tomcat
# processname: tomcat
# pidfile: /var/run/tomcat.pid
export JAVA_HOME=/usr/lib/jvm/java-6-sun
case $1 in
start)
        sh /usr/local/tomcat/bin/startup.sh
        ;;
stop) 
        sh /usr/local/tomcat/bin/shutdown.sh
        ;;
restart)
        sh /usr/local/tomcat/bin/shutdown.sh
        sh /usr/local/tomcat/bin/startup.sh
        ;;
esac
exit 0
You’ll need to make the script executable by running the chmod command:
sudo chmod 755 /etc/init.d/tomcat
The last step is actually linking this script to the startup folders with a symbolic link.
Execute these two commands and we should be on our way.
sudo ln -s /etc/init.d/tomcat /etc/rc1.d/K99tomcat
sudo ln -s /etc/init.d/tomcat /etc/rc2.d/S99tomcat
Tomcat should now be fully installed and operational. Enjoy!