Rake 0.9 breaks. !!

Recently I was doing a rails project where I did a normal

rake db:migrate

I got an error like this:

rake aborted! uninitialized constant Rake::DSL /usr/lib/ruby/1.9.1/rake.rb:2482:in `const_missing' 
/usr/lib/ruby/gems/1.9.1/gems/rake-0.9.0/lib/rake/tasklib.rb:8:in `<class:TaskLib>' 
/usr/lib/ruby/gems/1.9.1/gems/rake-0.9.0/lib/rake/tasklib.rb:6:in `<module:Rake>' 
/usr/lib/ruby/gems/1.9.1/gems/rake-0.9.0/lib/rake/tasklib.rb:3:in `<top (required)>' 
/usr/lib/ruby/gems/1.9.1/gems/rake-0.9.0/lib/rake/rdoctask.rb:20:in `require'
 /usr/lib/ruby/gems/1.9.1/gems/rake-0.9.0/lib/rake/rdoctask.rb:20:in `<top (required)>' 
/usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/tasks/documentation.rake:1:in `require' 
/usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/tasks/documentation.rake:1:in `<top (required)>'
 /usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/tasks.rb:15:in `load'
 /usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/tasks.rb:15:in `block in <top (required)>' 
/usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/tasks.rb:6:in `each' 
/usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/tasks.rb:6:in `<top (required)>' 
/usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/application.rb:214:in `require' 
/usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/application.rb:214:in `initialize_tasks' 
/usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/application.rb:139:in `load_tasks' 
/usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/application.rb:77:in `method_missing' 
/home/mikhail/test_app/Rakefile:7:in `<top (required)>' /usr/lib/ruby/1.9.1/rake.rb:2373:in `load' 
/usr/lib/ruby/1.9.1/rake.rb:2373:in `raw_load_rakefile' /usr/lib/ruby/1.9.1/rake.rb:2007:in `block in load_rakefile' 
/usr/lib/ruby/1.9.1/rake.rb:2058:in `standard_exception_handling' /usr/lib/ruby/1.9.1/rake.rb:2006:in `load_rakefile'
 /usr/lib/ruby/1.9.1/rake.rb:1991:in `run'  /usr/bin/rake:31:in `<main>'.
I had no clue what this error was. Then I (ofcourse) googled it and saw this tweet  by DHH and found the fix. Actually there is some problem in rake 0.9 so use:
gem "rake", "0.8.7" in your rake file.  

A SAMPLE PROGRAM TO PERFORM REGRESSION ON 2 SERIES

The formula used is :

Regression Equation(y) = a + bx 
Slope(b) = (NΣXY – (ΣX)(ΣY)) / (NΣX2 - (ΣX)2)
Intercept(a) = (ΣY – b(ΣX)) / N

#this is a simple program to perform linear regression on 2 sequences x and y

#the 2 sequences will be accepted as arguments in the function

#v is the value of x for which we want to find value of y

class Linear_regression

  def linear_calculate(x,y,v)

    #the linear regression equation is

    #y=a+bx

    #b is slope and a is intercept

    #number of samples

    n=x.length

    #calculation of summation of x,y,x2,y2and xy

    x_sum=0

    y_sum=0

    x2_sum=0

    y2_sum=0

    xy_sum=0

    for i in 0..x.length-1

      x_sum=x_sum+x[i]

      y_sum=y_sum+y[i]

      xy_sum=xy_sum+x[i]*y[i]

      x2_sum=x2_sum+x[i]**2

      y2_sum=y2_sum+y[i]**2

    end

    #end of i for loop

    #calculate slope b

    b1=(n*xy_sum-(x_sum*y_sum))

    b2=((n*x2_sum)-(x_sum**2))

    b=b1/b2

    #calculate intercept a

    a=(y_sum-(b*x_sum))/n

    #the regression equation is :

    puts  “the regression equation is :”

    puts “y = “+a.to_s+”+”+b.to_s+”x”

    r=a+b*v

    #the answer for x=v

    puts “The final answer is “

    puts r.to_s

  end

    #end of method linear_calculate

end

#end of class linear_regression

x=Array.[](60,61,62,63,65)

y=Array.[](3.1,3.6,3.8,4,4.1)

Linear_regression.new.linear_calculate(x,y,64)

A SAMPLE PROGRAM TO CALCULATE CORRLATION BETWEEN 2 SERIES

 

The program is self-explanatory and comments will be helpful

The formula used is :

#This is a program to find correlation between 2 sequences x and y

#In this program , x and y will be arrays passed as arguments to the function

class Correlation

def corr_calculate(x,y)

#first lets fnd mean of x and y

x_mean=0

y_mean=0

for i  in 0.. x.length-1

x_mean=x_mean+x[i]

y_mean=y_mean+y[i]

end

x_mean=x_mean/x.length.to_f

y_mean=y_mean/y.length.to_f

#end of i for loop

#This is the code for calculating the standard deviation of x and y

y_sd=0

x_sd=0

for k in 0..x.length-1

x_sd=x_sd+(x[k]-x_mean)**2

y_sd=y_sd+(y[k]-y_mean)**2

end

x_sd=x_sd/x.length

y_sd=y_sd/y.length

x_sd=Math.sqrt(x_sd)

y_sd=Math.sqrt(y_sd)

#Thus we calculated the standard deviation of x and y

#now we calculate numerator for corrleation

num=0

for j in 0..x.length-1

num=num+x[j]*y[j]

end

num=num-(x.length*x_mean*y_mean)

#thus we calculate numerator

#now we calculate denominator for corrleation

den=(x.length)*x_sd*y_sd

#thus we calculate denominator

#finally we calculate correlation

corr=num/den

puts “The co-relation is : “+corr.to_s

end

#end of corr_calculate method

end

#End of correlation class

x=Array.[](68,71,62,75,58,60,67,69,71,69,68,67,63,62,60,63,65,67,63,61)

y=Array.[](4.1,4.6,3.8,4.4,3.2,3.1,3.8,4.1,4.3,3.7,3.5,3.2,3.7,3.3,3.4,4.0,4.1,3.8,3.4,3.6)

Correlation.new.corr_calculate(x,y)

x=Array.[](1,2,3,4,5)

y=Array.[](1,2,3,4,5)

Correlation.new.corr_calculate(x,y)

A SIMPLE PROGRAM TO CALCULATE GEOMETRIC MEAN OF A GIVEN SEQUENCE IN RUBY LANGUAGE

In this program the practical definition of a geometric mean is used.

The definition is : The average of the logarithmic values of a data set, converted back to a base 10 number.

I have found that base 10 is not required.

I have used base 2 and as far as I know, anything can be used.

The program has comments and since it’s a fairly simple program , its self explanatory

#This is the program to calculate geometric mean of a sequence.

#The sequence will be passed as an array as argument to the function

class Geometric

#this is the method to return the geometric mean of the sequence

def mean_calculate(x)

tmp=0

y=Array.new       #This array will be used to store logarithms of the numbers of the sequence

#this loop is used to find logs of numbers in sequence and sum them up in a variable called tmp

for i in 0..x.length-1

y[i]=Math.log(x[i])

tmp=tmp+y[i]

end

#end of i for loop

#this is to find average of the sum of logs taken.

avg_log=tmp/x.length

#This is to convert logs back to original form

gm=Math.exp(avg_log)

#Finally display the result !!

puts “Geometric mean is : “+gm.to_s

end

#end of mean_calculate method

end

#end of class

x=Array.[](6,50,9,1200)

Geometric.new.mean_calculate(x)

In this code, I have used a very simple sequence.

You may test it with a larger sequence.

CONNECT 2 DATABASES IN A SINGLE RAILS APPLICATION

Many a times , the need of the situation is to connect ,multiple databases to a single rails app.

This seems to be a bit complex issue since we are all used to connect a single database to a rails app.

But after some googling, I found out that in rails, it’s a damn simple thing.

I will show you how I did it.

I have a rails app called “multidbtest1”.

It has a table called as User.

I wanted to connect it to another database.So I created another app called “multidbtest3

It had a table called Customer.

My requirement is to use Customer table in multibdtest1.

We know that database connections are handled in database.yml file.

So we copy the contents of database.yml of multidbtest3 in database.yml of multidbtest1.

Thus, the database.yml file of multidbtest1 looks like:

# SQLite version 3.x

#   gem install sqlite3

development:

  adapter: sqlite3

  database: db/development.sqlite3

  pool: 5

  timeout: 5000

# Warning: The database defined as “test” will be erased and

# re-generated from your development database when you run “rake”.

# Do not set this db to the same as development or production.

test:

  adapter: sqlite3

  database: db/test.sqlite3

  pool: 5

  timeout: 5000

production:

  adapter: sqlite3

  database: db/production.sqlite3

  pool: 5

  timeout: 5000

—————————-copied content of multidbtest3—————————-

 #SQLite version 3.x

#   gem install sqlite3

development:

  adapter: sqlite3

  database: db/development.sqlite3

  pool: 5

  timeout: 5000

# Warning: The database defined as “test” will be erased and

# re-generated from your development database when you run “rake”.

# Do not set this db to the same as development or production.

test:

  adapter: sqlite3

  database: db/test.sqlite3

  pool: 5

  timeout: 5000

production:

  adapter: sqlite3

  database: db/production.sqlite3

  pool: 5

  timeout: 5000

But this seems to be obviously wrong, so we make some changes.In the database field , we must specify the name and path of the database.The modifies file looks like:

# SQLite version 3.x

#   gem install sqlite3

development:

  adapter: sqlite3

  database: db/development.sqlite3

  pool: 5

  timeout: 5000

# Warning: The database defined as “test” will be erased and

# re-generated from your development database when you run “rake”.

# Do not set this db to the same as development or production.

test:

  adapter: sqlite3

  database: db/test.sqlite3

  pool: 5

  timeout: 5000

production:

  adapter: sqlite3

  database: db/production.sqlite3

  pool: 5

  timeout: 5000

 

-—————————copied content of multidbtest3—————————-

 #SQLite version 3.x

#   gem install sqlite3

multidbtest3_development:

  adapter: sqlite3

  database: C:/Users/Rushabh/RubymineProjects/multidbtest3/db/development.sqlite3

  pool: 5

  timeout: 5000

# Warning: The database defined as “test” will be erased and

# re-generated from your development database when you run “rake”.

# Do not set this db to the same as development or production.

multidbtest3_test:

  adapter: sqlite3

  database: :/Users/Rushabh/RubymineProjects/multidbtest3/db/test.sqlite3

  pool: 5

  timeout: 5000

multidbtest3_production:

  adapter: sqlite3

  database: :/Users/Rushabh/RubymineProjects/multidbtest3/db/production.sqlite3

  pool: 5

  timeout: 5000

That’s all!!  The database part is done.

Now how shall we use the database ?

Its even simpler.

Create a model class called “Customer”

The model file should look like :

class Customer < ActiveRecord::Base 

  establish_connection “multidbtest3_#{Rails.env}” 

end

That’s it.

Now in any controller we can access the contents of Customer table.

For eg :

@customers = Customer.find(:all)

Enjoy

DEPLOY A RAILS 3 APP IN RACKSPACE

EDIT: TO INSTALL MYSQLITE AND SQLITE3 USE THE FOLLWING:

yum install mysql-server mysql-libs mysql-devel
yum install sqlite sqlite-devel
THIS INSTALLS THE LIBRARIES
gem install sqlite3-ruby
THIS INSTALLS THE GEM

The steps to setup/deploy your rails application on a rackspace cloud server are given below. The steps given below worked for me. Along with the steps I am also putting up screenshots where I think they are necessary.

  • Login to the control panel of your account.
  • On the screen that appears after successful login, on the left habd side, click HostingàCloud Servers àAdd server

  • Then select the Operating system that you want.I selected fedora 14 and steps given below are for fedora 14.
  • Then select the type of RAM/Disk and fill in credentials like name etc.
  • Then you will see a screen like this:

 

Now you are ready to go.Now we need to setup rails on our server.You an click on console above and get started.My suggestion would be to use a ssh client like puTTy which can be downloaded from here.

  • Install the linux dependencues and required libraries:
sudo yum -y install gcc gcc-c++ make zlib zlib-devel openssl openssl-devel git expect pcre pcre-devel readline-devel mysql mysql-devel libxml2 libxml2-devel libxslt libxslt-devel
  • To install ruby 1.9.2 use the following snippet:
  1. wget ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.2-preview3.tar.gz
  2. tar -xvf ruby-1.9.2-preview3.tar.gz
  3. cd ruby-1.9.2-preview3
  4. ./configure
  5. make
  6. sudomakeinstall
  7. cd ..
  • Done.This will install ruby 1.9.2 in your system(server).To check type the following at the terminal:

 ruby -v

 

  • To install rubygems use the following (I installed version 1.3.7 and then updated it)
    • The latest version can be downloaded from  This link
  1.  wget http://production.cf.rubygems.org/rubygems/rubygems-1.3.7.tgz
  2. tar -xvf rubygems-1.3.7.tgz
  3. cd rubygems-1.3.7
  4. sudo ruby setup.rb
  5. cd ..

  This should install rubygems to the system. To update it, use  

gem update –system

 

  • To install rails use the simple command
 gem install rails

this will install rails 3 in your system.

  • Create a directory  and make a blank rails application:
  1. mkdir  my_sample_rails_apps
  2. cd my_sample_rails_apps
  3. rails new test1

 

  • This will create a blank rails app.Now we can transfer an exisiting rails app using githhub.

To use github,we must have an account there.

  • In the new system, create a ssh key using:
ssh-keygen -t rsa -C youraddress@email.com

 The full tutorial to generate a key and transfer your application to git hub remote repository can be found at :  github tutorial for ssh keys

  • Now we can copy our project from github to our server using:
git clone git@github.com:your_username/your_project.git

now the whole project is in a directory called ‘your_project’(replace it with the name of your project)

  • Go in that directory using cd and then type :
 rails server

AND THE MAGIC BEGINS

 

I WILL BE COVERING   HOW TO MIGRATE DATABSES AS GITHUB DOES NOT MIGRATE DATABSES IN THE  NEXT POST.(COMING SOON)

Postgre SQL

  •  FEATURES OF POSTGRESQL:

PostgreSQL supports all of the features you would expect from an open source RDMS, including many that are found in commercial databases (Oracle, DB2, SQL Server) such as:

  • The basics: viewstriggersindexesforeign keys, ACIDity, transactionsquery optimization, comprehensive SQL support and data typesautovacuum (keeps your table statistics up to date).
  • The not so basics: Features that may not be seen on other DBMSes include reverse, partial and expression indexes, table partitioning, table inheritance, cursors, data domains, user-defined operators, arrays and regular expressions.
  • Procedural Languages: analogous to Oracle’s PL/SQL or SQL Server’s T/SQL, PostgreSQL supports internal procedural languages for when you need to get down and dirty with the data. Additionally, it supports a wide range of languages including Ruby.
  • Rules, which pretty much allow you to rewrite an incoming query. A typical use of Rules is to implement updatable views.
  • Multi-Version Concurrency Control: MVCC is how PostgreSQL (and other DBMSes) deal with concurrency and table locking. In practical terms, it allows for database reads while the data is being writen.
  • Write-Ahead-Log: the WAL is the mechanism by which PostgreSQL writes transactions to the log before they are written to the database. This increases reliability in the unlikely event of a database crash, as there will be a transaction log by which to rebuild the database’s state. PostgreSQL includes many configuration parameters to tweak the behavior of the WAL.
  • PostgreSQL scales up by efficiently using multi-core servers. It also sport an asynchronous processing API. Subselects are fast, you can refer to tmp tables more than once in a query and it can use more than one index per query, making it suitable for data warehousing applications as well.
  • tsearch2, which is PostgreSQL’s highly efficient full text search component. If you are committed to PostgreSQL, this is a very high performant search engine for PostgreSQL (as an alternative to solr orsphinx, for instance), with the added benefit that you’re not running a separate daemon or search service.
  • PostGIS for geospacial objects.
  • There are many replication options, although non of them are built into the core. This will change very soon, though.
  • You can tweak its brains out: open up postgresql.conf, and you’ll find many configuration options that can be tweaked to your application load and server capabilities. If you’re like me, this is lots of fun. I will say, however, that it will take time to understand many of the options and how they affect each other.
  • Excellent documentation, which not only goes through the basics of setting up and using PostgreSQL, but really gets into the details of the inner workings of the system. This is an invaluable resource, not only for day-to-day development but also for tweaking the database’s configuration files.
  • Much more in the contrib packages.
  •  ADDITIONAL INFORMATION LINK:

postgresql wiki

  • DOWNLOAD POSTGREsql :

download/

  •  MAJOR DIFFERENCES FROM MYSQL:

MYSQL Vs Postgresql

Why is Ruby on Rails preferred over JAVA and PHP?

  •  Ruby on Rails is more object-oriented than PHP, thus providing all the inherent advantages of an object-oriented language such as simplicity, modularity, modifiability, extensibility, maintainability and re-usability.
  • Ruby on Rails is a web framework whereas PHP is only a scripting language.
  • Ruby on Rails includes a web server for development, whereas a web server has to be installed for PHP.
  •  Ruby on Rails implements the Model-View-Controller (MVC)architecture, whereas the MVC architecture has to be implemented externally in PHP.
  • Ruby on Rails framework provides the Create-Read-Update-Delete (CRUD) functionality, PHP doesn’t.
  •  Java has advantages over Ruby on Rails in scalability, security,transaction management and availability of development tools.
  • Ruby onRails has advantages over Java in simplicity, development speed, and maintainability.
  • Ruby on Rails provides a tightly coupled web framework consisting of a persistence framework, a web application framework, and a Web Services framework. The J2EE application framework is not as seamless as Ruby on Rails and requires much more configuration.
  •  Ruby on Rails also has the advantage of being dynamically typed, while Java is statically typed.

 

ONE IMPORTANT THING WHICH MUST BE NOTED ACCORDING TO ME IS THAT WE CAN HAVE A JAVA OR PHP DEVELOPER AT A STONES THROW BUT THAT’S NOT THE CASE WITH RUBY ON RAILS.

Financial Planning

what is financial management?

Introduction

Financial Management can be defined as:

The management of the finances of a business / organisation in order to achieve financial objectives

Taking a commercial business as the most common organisational structure, the key objectives of financial management would be to:

• Create wealth for the business

• Generate cash, and

• Provide an adequate return on investment bearing in mind the risks that the business is taking and the resources invested

There are three key elements to the process of financial management:

(1) Financial Planning

Management need to ensure that enough funding is available at the right time to meet the needs of the business. In the short term, funding may be needed to invest in equipment and stocks, pay employees and fund sales made on credit.

In the medium and long term, funding may be required for significant additions to the productive capacity of the business or to make acquisitions.

(2) Financial Control

Financial control is a critically important activity to help the business ensure that the business is meeting its objectives. Financial control addresses questions such as:

• Are assets being used efficiently?

• Are the businesses assets secure?

• Do management act in the best interest of shareholders and in accordance with business rules?

(3) Financial Decision-making

The key aspects of financial decision-making relate to investment, financing and dividends:

• Investments must be financed in some way – however there are always financing alternatives that can be considered. For example it is possible to raise finance from selling new shares, borrowing from banks or taking credit from suppliers

• A key financing decision is whether profits earned by the business should be retained rather than distributed to shareholders via dividends. If dividends are too high, the business may be starved of funding to reinvest in growing revenues and profits further.

 

original source:http://tutor2u.net/business/accounts/finance_management_intro.htm

Use of spreadsheet gem to integrate rails and excel.

STEPS TO READ AND WRITE DATA IN EXCEL SHEETS FROM A RAILS APP.

Many a times,we run in a situation where we have to integerate our rails app and and excel worksheet.

Here is a simple step by step guide to do the same.

First step is to install a gem that I will be using.

The gem is called “Spreadsheet”.

The readme for the same can be found here

To install the gem use :

gem install spreadsheet.

(note : in rails 3 use gem ‘spreadsheet’ in the gem file and then do bundle install)

Once the gem is installed, one can open the controller action where the data is to be written to the ex cel file.

In my case the action was create.

The code for the same is :

def create       

 require ‘spreadsheet’                    

# include libraries

 @sample = Sample.new(params[:sample])

      #sample is my model name

 

    respond_to do |format|

 if @sample.save

book = Spreadsheet::Workbook.new  

 # create a new excel file

sheet1 = book.create_worksheet      

#initialize a new sheet

 sheet1[1,1]=params[:sample][:name]

#insert data in required sheet([row][column]) its starts from [0][0]

  book.write ‘C:/TC/test.xls’              # commit the write

 

 

(Note : written in blue are the comments and in black is the code)

 

Similarly to read data from excel files we use the same gem as:

 

def index

require ‘spreadsheet’                                   

#include the libraries.

  book = Spreadsheet.open ‘C:/TC/test.xls’

#open the required file

  sheet1=book.worksheet 0

#open the required sheet

  @name=sheet1[1,1]

#read the data