Posts

Showing posts from October, 2011

Different types of joins in SQL

Inner Join - Inner join creates a new result table by combining column values of two tables (A and B) based upon the join-predicate. The query compares each row of A with each row of B to find all pairs of rows which satisfy the join-predicate. When the join-predicate is satisfied, column values for each matched pair of rows of A and B are combined into a result row. The result of the join can be defined as the outcome of first taking the Cartesian product (or Cross join) of all records in the tables (combining every record in table A with every record in table B)—then return all records which satisfy the join predicate

Inner joins is further classified as equi-joins, as natural joins, or as cross-joins.

Equi-join
An equi-join, also known as an equijoin, is a specific type of comparator-based join, or theta join, that uses only equality comparisons in the join-predicate. Using other comparison operators (such as <) disqualifies a join as an equi-join


Natural join

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 the…

Difference between Application server and Web Server

Apache, nginx, IIS are web servers 

Mongrel, Webrick, Phusion passenger are application servers

Application 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

Linkedin Gem for Rails application

This is an example to use Linkedin Gem for using linkedin api inside your rails application

If you wanted to replace these things APP_CONFIG['api_key'] with your api keys you can also do that.

These are the things that are fetched by the linkedin gem using the api

first-name last-name headline positions educations skills

You can use @profile to fetch the values and use them according to your requirement. class UserController < ApplicationController def login client = LinkedIn::Client.new(APP_CONFIG['api_key'], APP_CONFIG['secret_key']) request_token = client.request_token(:oauth_callback => "http://#{request.host_with_port}/user/authenticate_login") session[:rtoken] = request_token.token session[:rsecret] = request_token.secret redirect_to client.request_token.authorize_url end def profile end def authenticate_login client = LinkedIn::Client.new(APP_CONFIG['api_key'], APP_CONFIG['secret_key'…

Best Way to Define API keys for Rails application

First Create a YML file inside the config folder of the application


your_file_name.yml development: api_key: "your api key" secret_key: "your secret key"

Then create a file in config/initializers folder , for example load_keys.rb and paste the following code inside that
load_keys.rb APP_CONFIG = YAML.load_file("#{RAILS_ROOT}/config/your_file_name.yml")[RAILS_ENV]
Now you can use these inside your application
APP_CONFIG['api_key'] APP_CONFIG['secret_key']

Version Control with Git

Rails developers as practically essential, namely, placing our application source code under version control. Version control systems allow us to track changes to our project’s code, collaborate more easily, and roll back any inadvertent errors (such as accidentally deleting files). Knowing how to use a version control system is a required skill for every software developer.

First-Time System Setup
After installing Git, you should perform a set of one-time setup steps. These are system setups, meaning you only have to do them once per computer:

$ git config --global user.name "Your Name"
$ git config --global user.email youremail@example.com

I also like to use co in place of the more verbose checkout command, which we can arrange as follows:


$ git config --global alias.co checkout


First-Time Repository Setup
Now we come to some steps that are necessary each time you create a new repository (which only happens once in this book, but is likely to happen again some day). First n…

Ruby on Rails : A Brief Development Overview

Ruby on Rails is the most popular and agile web application development framework written using Ruby programming language. Ruby on Rails often called RoR or Rails is an open source web development framework and an object oriented programming language that helps to develop simple, complete and powerful web applications with rich interactivity along with the latest functionalities.

RoR is developed using the Model-View-Controller Design Pattern and it is favourite among Ruby on Rails developers because of its philosophy of CoC (Convention over Configuration), DRY (Don't Repeat Yourself), and close association with the agile development methodology. The main advantage of using Ruby on Rails is the agile nature of development, what takes months on other platforms, takes only week to develop on Ruby on Rails. Ruby on Rails was first extracted from Basecamp by David Heinemeier Hansson in July 2004 and it is really easy to deploy web solutions using Rails as it works well with a wide …