Storing a simple student database in Rails

Set up Ruby on Rails from http://rubyonrails.org/download. Make a directory ‘my_rails’ in your home folder for rails applications.
$ cd my_rails

create your application as following
$ rails new student_db
$ cd student_db
For including gems, you need to update the Gemfile, that resides in your application folder. Open the ‘Gemfile’ and update.

Gemfile :
source ‘http://rubygems.org’
gem ‘rails’, ‘3.0.0’
gem ‘sqlite3-ruby’, ‘1.2.5’, :require => ‘sqlite3’

Specify the version number for sqlite3.    This is for avoiding confusion while installing the gems.

Install gems
$ bundle install

Now you are ready to generate the student model. Use scaffold generator.

Student data model

Note : Rails developers dislike scaffold. Because it itself generates code. So don’t use it. This is only for referring the application.
$ rails generate scaffold Student first_name:string last_name:string roll_no:integer total_mark:integer

Migrate the database using rake.

$ rake db:migrate

Now run the server.

$ rails s

open the browser and go to http://localhost:3000/student

To see this application visit,
Student_data_base

Advertisements

Deploying Rails application in Heroku

Sign up using e-mail and a password.
http://heroku.com/
Then Heroku asks to check our mail and continue. Install git. Then Install heroku gem.

$ gem install heroku
Create SSH keys and tell Heroku the public key by
$ heroku keys:add
Create a new application in Heroku
$ heroku create
Then Heroku creates a random sub domain for our application. We can change it as we like. Then deploy the app to Heroku by
$ git push heroku master
Now we can visit our application by using the address.

Heroku pricing plans

Heroku provides different plans to deploy the application and for using the database space. Here the plan is Blossom. It is free and can take 5MB database maximum.

Git – The version control system

Git is a version control system and a very good friend of a developer. Todays all projects are very much depend on git. What’s the use of git? If a programmer develops a project he want to take copies of the progressing project at stage by stage. Using git we can ‘commit’ the project by giving comments like ‘first commit’ , ‘second commit’ etc. When a programmer deletes a file, he can recover the file using git. Git uses a tree structure to hold the data. It has a parent repository called master branch. We can change the branch to another and can process the data. Multiple branches are using when working on a projects with multiple developers. This branch master is unaltered when working on another branch. Download .rpm packages from RPM packages, and .deb packages from Debian Packages

Turtle graphics programming in python

Using python’s tkinter module we can create a logo style programs. To do this in python import the module tkinter. Draw the class turtle. Initialize the turtle class with coordinates and angle. Write a function draw_turtle. Draw three straight lines to form a triangle. Write another function delete_turtle to delete the lines. To move forward write mv_fwd function with a parameter ‘distance’. In this function first delete the turtle, draw a line to that distance and redraw the turtle. Preserve the angle.

Turtle graphics in python

Visit my github location for source code – http://github.com/abhilashak/logo_in_python

To rotate write rt_lt and rt_rt functions. First delete the existing turtle. From the angle this function calculates the position of new turtle. Redraw it.  rt_rt is just the negative angle of rt_lt.

My project in Web application field – Graph colouring Algorithm

Today the web applications have a great value. Anyone can access the application and do their jobs through the web. Hosting the application require a server, that manages the request and stores the data. Here the google appengine does the job. Google app engine is a cloud environment that provides the server-side services we want. Here the application is a graph colouring technique which takes a graph and provides a proper colour to each of the nodes according to the vertex colouring algorithm. This algorithm is implemented in python.

The front end of the project is a HTML5 page. It uses the web application languages javascript and jquery. Here the front-end consists of a html5 canvas and the control buttons. We can draw an undirected graph in the canvas using the control buttons. These buttons are created in html5 page. The program code resides in two files. One html5 file and one javascript file.

My graph colouring canvas

A html5 canvas is created using the canvas tag in html5 file. It contains the id and the size of the canvas. Using this id we can obtain the context of the canvas. Specifing this context it creates, the colour of the canvas, style etc. The html5 page displays the canvas and the buttons when it loads.

Visit my github location to get the project code : http://github.com/abhilashak/project_graph_color

Visit my application at : http://colourthegraph.appspot.com/canvas/se11.html
Usually the term graph colouring algorithm indicates the vertex-colouring algorithm even though other algorithms are exists such as edge-colouring algorithm. The input of the graph colouring algorithm is the adjacentcy list of each node. It gives the nodes with the proper colour.

This Algorithm keeps a set of colours and the ‘availability list’ of colours for each node. First it takes each node in the order. It then checks the adjacentcy list of that node. If the first node in the list is coloured, it deletes that colour from the availability list. Then takes the next node from the adjacentcy list and the process continues. Last assigns the first colour in the availability list. This ensures the algorithm to take smallest number of colours.

What is padding in C?

In C what is meant by padding of bits? Consider the following structure,

struct number {
int a;
char b;
int c;
};


What is the size of this structure? An integer takes 4 bytes, char takes 1 and the third integer 4 bytes. Total 9 bytes. This is wrong. We can see the size of the structure is 12 bytes. What happens here? the compiler pads 3 bytes to char element. This is for proper alignment. In a 32 bit processor it checks the next data only after 32 bits. Else the compiler will have related problems. Now the compiler knows each data is 4 bytes wider. So the benefits.

Is the study of pointers in C is easy?

Here explains a little about pointers.  Yes the pointers are easy, of course. But take care in your building path. If you go through pointers deeply, you will see the complexity.

In C address is only for l-values. It can be a variable or something. If we assign p = &a, the type of p is int * – pointer to an integer. type of ‘a’ is int and type of ‘&a’ is int *. C language is a weakly typed language. ie when we use assignment,  C allows to assign the right most thing to the left most without checking the types of these two.

int *p;
p = 1;
*p = 10;
Consider the above statements in C. We declared p as a pointer variable. Then 1 is assigned to p. In the third statement 10 is assigning to the memory location 1. What about the memory location 1, is there exists a memory address 1. Usually not. If yes, of course it isn’t accessible. OS allows the program to access only within a particular memory location. This is Memory protection. If OS takes no care about memory protection what happens? In that system so many programs are running and two programs want to store data on say 2000 th location then what happens? The programs access wrong data.

int *p;
p = 0;
*p = 10;

By executing above statements there is no error at compile time. Why? C takes zero as a pointer.

Its easy to go further.