Setting up Ruby on Rails with Passenger and Apache on a fresh new Ubuntu Box (AWS EC2)

I’ve been setting up multiple Ubuntu machines from past 1 year and so writing down the steps that I have compiled after hours of frustration and research.

DISCLAIMER : There are steps like these on many blogs but they did not work for me (hence this list). The steps mentioned below may also not work for you.

  1. gpg –keyserver hkp:// –recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
  2. \curl -sSL | bash -s
  3. logout and login again (close the terminal and open it again)
  4. type this on the terminal window : source ~/.rvm/scripts/rvm
  5. Check if RVM was installed properly, type : rvm | head -n 1 (output should be “rvm is a function”)
  6. rvm install 2.2
  7. rvm use 2.2
  8. sudo apt-get install nodejs
  9. sudo apt-get install git
  10. sudo apt-get install libmysqlclient-dev
  11. gem install mysql2
  12. gem install bundler

After this point, you should set up your directories with your code in the server. Once you do that then do these steps

  1. bundle install (do it in all the code directories)
  2. sudo apt-get install apache2
  3. sudo apt-get install libcurl4-openssl-dev
  4. sudo apt-get install apache2-threaded-dev
  5. gem install passenger (first try without sudo, if it  fails then prepend “sudo” and run the command again)
  6. passenger-install-apache2-module (if you used sudo in the above step, use sudo here, else don’t)

Now sit back and relax or grab a cup of coffee. Passenger will take around 10-15 mins (depending on your internet connection speed) to finish.

Once its done, it will print a configuration on the screen. Copy that and do the below step

  1. cd /etc/apache2
  2. sudo nano apache2.conf
  3. go the bottom of the conf file and paste the lines you copied.
  4. Now do this : cd /etc/apache2/sites-available
  5. sudo nano 000-default.conf
  6. add the below lines at the end

<VirtualHost :80>
# Change these 3 lines to suit your project
RailsEnv development
DocumentRoot /your/website/path/public # Note the ‘public’ directory

Hope this helps!

Getting file name from full file path in SQL Server – UDF

Today at office I got a task which had a table with image URLs in one of the column. The image URLs were a network path. I had to write a script to identify the image name (file name) and then update it with a web URL for the same image. I needed a function which would give me file name when given a file path. As you must know, such type of things is very easy in C#. But in SQL Server we do not have any in built function to do this. So I wrote a function of my own. Below is what I wrote & also showing how to use it. The script is self explanatory I believe J

CREATE FUNCTION [dbo].[udf_GetFileName]
	@FilePath VARCHAR(MAX),
	@Separator CHAR(1) = '\'
			LEN(@FilePath) - CHARINDEX(@Separator, REVERSE(@FilePath),1) + 2, -- From where to start
			LEN(@FilePath) - CHARINDEX(@Separator, REVERSE(@FilePath),1) -- how many characters to take

--How to use

DECLARE @networkpath VARCHAR(50) = '\\somenetworkpath\somefolder\networkfile.txt'
DECLARE @localpath VARCHAR(50) = 'D:\somefolder\localfile.txt'
DECLARE @webpath VARCHAR(50) = ''
	dbo.udf_GetFileName(@networkpath,DEFAULT) as [NetworkPath],
	dbo.udf_GetFileName(@localpath,DEFAULT) as [LocalPath],
	dbo.udf_GetFileName(@webpath,'/') as [WebPath]

The out put is as shown below


Hope it helps!

Opening Visual studio in older version after converting to a higher version

I have many times faced a problem of converting a Visual studio project to a higher version (say 2010) and then someone coming to me and saying that it needs to be in the older version only.

But problem with Visual studio is, once you convert from a lower to higher version, that project will not open back in the lower version. You may a error saying “The project was created using higher version and cannot be open”.

Well, i have found out a quick way to overcome this problem.

Every project file (.csproj OR .vbproj) is nothing but an XML File. When you convert a project, what it does is update few values in the XML file. What you need to do is, undo the changes done by the converter in the project XML file and you are good to go. To do that, Right click on the project file & edit it with your favorite editor (like notepad++).

Once you open it, search for the line shown in below screen shot.

Notice the “v9.0” this what tells Visual studio which version to use to open the project. So if you have converted a Project from VS 2008 to VS 2010, then this particular value would be “V10.0“. To open it back with VS 2008, just change it to “v9.0″ and then you should be able to open it with VS 2008.

Hope this quick & handy tip helps you :)

Technorati : , , , : , , ,

JQuery Rating plug-in – Not working in IE 8 – FIXED

Recently I was working on a web app where it was required to keep a Rating control (with 5 stars). Instead of re-inventing the wheel, I thought of using one of the existing plug-in. What can be better than a JQuery rating plug-in. I quickly went to the JQuery plug-in site here and studied the demo and implemented the script. It worked very smoothly!

But my joy did not last long L. The moment I opened my web page in IE 8, the rating control was not behaving properly, stars which were pre-selected (like loading the rating value from DB) used to come blank. The same was working in IE 7, but not in IE 8.

So I did some dissection on the JQuery Rating plug-in and modified it to work with IE 8. Below are the changes which I did,

Basically the problem was with this line,

if (0 !== $(“#” + id + ” option[selected]”).size())

IE 8 does not understand this tag option[selected]for that reason, always the “.size()” returns “0”. One option is to replace the above tag with option:selectedbut then our IE 8 will by default select the first element in the drop down (select) box and so even if no rating is given, the first star will be selected. This is IE’s behaviour. To overcome this problem, I modified the rating.js file,

var selectOptions = document.getElementById(id).getElementsByTagName("option");
    var selectOptionCount = 0;
    for (var i = 0; i < selectOptions.length; i++) {
        var sValue = selectOptions[i].className;
        if (sValue == "selected") {
            selectOptions[i].selected = true;

For above code to work, the only change you need to do is, instead of putting selected=”selected” for the selected item, put class = “selected” and the rating plug-in will automatically take care. This has been tested in IE 7, IE 8, Firefox 3.6.17, 4.0 & Chrome.

You can download the modified JQuery Rating Plug-in here

Hope it helps!

Using the IN Clause in Stored procedure – SQL Server

Yesterday, a junior developer came to me and told me that he converted a Stored Procedure to inline query because he was not able fetch data using the ‘IN’ clause from stored procedure. Let me demonstrate the problem using an example,

Let us first create a temporary table and create a stored procedure

The requirement was something like this,

But when used with stored procedure, it was not returning any records (see below)


Creating a function,

Altering the same stored procedure which we created earlier to use the function

The output,

–Run the same procedure again

usp_select_details ‘A1,A3,A5′

Clean up code,

WebService Studio – To the rescue

Recently at work I was working with couple of web services. After the services were developed we needed a way to test all those pages. What we thought that we can develop one generic page and that will help us to test all the services we developed. But that involved a lot of effort and unfortunately we were out of time. So what we did is we developed a test page for each of the services. But we ended with issues in the test page itself. Yesterday I thought of developing one single page for testing purpose and in that process I came across a tool called “WebService studio”. This tool was developed by Microsoft but now it is supported by CodePlex. This is one great tool, we just need to give the web service URL and it will show up all the web methods and their input parameters. One good thing is that, it also supports Custom objects as input. Still I am in process of finding its limitation but as of now this is savior for me. J

Updating a column value and making them all of one length

Today in office we had to run a job on a set of records given by client. The data was in form of an excel sheet which had some employee ids. We imported the data from excel to a table. We quickly found out that the employee ids in the excel sheet are not of the same length. For our job to run correctly it was required that all the values in the employee_id column be of length 8. As we were out of time, I quickly wrote 3 update statements to modify the employee id column. The update statements were as below,

UPDATE tbl_employee SET emp_id=‘000’+emp_id WHERE LEN(emp_id)=5
UPDATE tbl_employee SET emp_id=’00’+emp_id WHERE LEN(emp_id)=6
UPDATE tbl_employee SET emp_id=‘0’+emp_id WHERE LEN(emp_id)=7
We had three types of employee ids with length 5,6 & 7. It worked perfectly fine. But later I thought that this is not the right way to do. So I quickly did some findings and came up with a new single query which will do the job. Let me explain with an example,
CREATE TABLE tbl_employee(emp_id varchar(8),emp_fname varchar(25))
INSERT INTO tbl_employee(emp_id,emp_fname)
/* See values in tabels */
SELECT * FROM tbl_employee

/* We want to update all the emp_id so that all are of lenght 8 */
UPDATE tbl_employee SET emp_id=RIGHT(‘00000000’+emp_id,8)
/* See the new values in tables */
SELECT * FROM tbl_employee

Even Microsoft can be stupid :) (Installing silverlight tools for vs 2008)

Yesterday I was trying to install Silver Light tools for Visual studio 2008. I downloaded the pre requisites (will explain later in the post what all are pre requisite). When I went to Microsoft download center to download Silver Light tools for VS2008, it showed me a pop up saying “Please install Silver Light Plugin” and I did. After downloading the tools when I tried to install, it showed up an error “Fatal error, cannot continue”. After re searching a lot I got to know that I need to uninstall the silver light plugin before I can install Silverlight tools. Now that’s weird, Microsoft is only telling you to download the plugin when you visit the download section and then it tools fail because of plugin. Now that’s great! I hope Microsoft knows about this.

So here is what you need to have Silverlight tools for VS 2008.
1.       You should have VS 2008
2.       You should install VS 2008 SP 1. You can get it here.
3.       You should uninstall Silver light plugin before you continue.
4.       Then download Silver light tools for VS 2008. You can get it here.
If you follow the above 4 steps you will face no problems in getting silver light tools for VS 2008. Cheers !

Installing Windows 7 ? Read this before you continue.

It’s been a long time now since Windows 7 is out. Many of us must have already installed Windows 7 and must have started using. This post is for those who are planning to install Windows 7 in near future. When you install any version of Windows 7 (Home Basic/Premium, Professional or Ultimate) you get an option to choose between 32-bit or 64-bit. Before making a choice you need to know what exactly 32-bit and 64-bit OS means.

X-bit OS refers to the maximum memory your operating system can allocate to any application. The maximum memory address your operating system can assign is always 2^X. So if you have a 32 bit operating system your OS can assign maximum of 4GB (2^32 = 4294967296 Bytes / 1024 = 4194304 KB / 1024 = 4096 MB /1024 = 4 GB). So if you have a 32 bit OS it means that your OS cannot allocate memory to any application more than 4 GB. So if you have a RAM of 8 GB and you have a 32 bit OS then your OS never allocates memory beyond 4 GB. In that case your remaining 4 GB memory is never used. As long as you have < 4GB memory I would suggest you to install 32 bit OS. 64 bit OS is only for those who has more than 4 GB of memory, then only you can have the advantage of a 64 bit OS.
My personal experience: There are many softwares which still supports only 32 bit OS. It will take some amount of time for all software in market to support 64 bit OS. So I suggest going with only 32 bit OS.