Tuesday, 3 July 2012

mongodb tutorial

MongoDB is an open source, high-performance, open source NoSQL database.

Compared to relational DBMS, Mongodb is quite different. In MongoDB you store JSON-like documents with dynamic schemas.


Install mongodb depending on the OS you are working on.

Here is the link where you can download them

http://www.mongodb.org/display/DOCS/Quickstart

I have Installed mongodb directly from ubuntu software center.

after installation restart the mongo server

> sudo service mongodb start

You may prompted with "start: Job is already running: mongodb". This means mongo server was already running.

run the below command to connect to mongodb

> mongo

MongoDB shell version: 1.8.2
Tue Jul  3 12:41:59 *** warning: spider monkey build without utf8 support.  consider rebuilding with utf8 support
connecting to: test

In the above case mongodb by default connect to test database.

lets now create your first record as below

> db.test.save({a: 1})

The above command will create record in database. To view the record just run the find statement as below

> db.test.find({a: 1})
{ "_id" : ObjectId("4ff29c16ab6b02ddb38d978a"), "a" : 1 }


create second record by adding another column parameter

> db.test.save({b: 1, name: 'uma mahesh'})

to view the record

> db.test.find({b: 1})
{ "_id" : ObjectId("4ff29c41ab6b02ddb38d978b"), "b" : 1, "name" : "uma mahesh" }


to find all the records that were created, just run the below command

> db.test.find()

{ "_id" : ObjectId("4ff29c16ab6b02ddb38d978a"), "a" : 1 }
{ "_id" : ObjectId("4ff29c41ab6b02ddb38d978b"), "b" : 1, "name" : "uma mahesh" }


To view list of all databases in mongodb, run the below command

> show dbs

Thsi will display list of databases as below

admin    (empty)
local    (empty)
test    0.0625GB




Thank You,
Uma Mahesh.

Sunday, 1 July 2012

remove trailing whitespace before git commit

Hi,

Trailing whitespace always makes noises in version control system. We should remove trailing whitespace to avoid annoying other team members.

While working with remote developers we should follow some coding standarts and should form a habit to remove trailing whitespace before committing.


We can do it by git pre commit hook or deleting trailing whitespace by our own.

I hope all the IDE will support the code re-factoring. I am using rubymine and I used to refactotr code as below.

In rubymine

> On the main menu, choose Code | Reformat Code, or press Ctrl+Alt+L.

> In the Reformat Code dialogue box, specify the reformatting scope:
The current file.
Selected text.
All files in the current directory, including or omitting subdirectories.

> Click Run.


Thank You,
Uma Mahesh.

Friday, 29 June 2012

hash syntax change in ruby 1.9

Hi,

In ruby 1.9 hash syntax has been changed. Use the new syntax to over come the problem while upgrading some gems or plugins to future versions.

# Ruby 1.8 Syntax
{:name => 'uma mahesh', 'phone' => '9866439593'}
# Ruby 1.9 syntax
{name: 'uma mahesh', 'phone': '9866439593'}


hash keys should accessed with symbols as below

profile = {:name => 'uma mahesh', 'phone' => '9866439593'}

profile['name'] # => nil

profile[:name] => 'uma mahesh'

The new syntax mimics JSON which is important for Ruby, or at least Rails, developers as JavaScript is one of the languages we’ll switch to more frequently.


Its is similarity to JavaScript's object notation, and looks a bit like JSON



Thank You,
Uma Mahesh.

creating indexes for all database tables on fly in rails

Hi,

Indexing plays a major role in database query optimisation. So it is suggested to have index for foreign_key columns.

Coming to rails migrations, our migration will not create indexes for the columns with foreign keys. Where you need to create manually those indexes.


Normal migration will be as below

class CreateArticle < ActiveRecord::Migration
  def self.up
    create_table "articles" do |t|
      t.string :content
      t.integer :post_id
      t.integer :user_id
    end
  end

  def self.down
    drop_table "articles"
  end
end


Inorder to add indexes, add the indexes as below:

class CreateArticle < ActiveRecord::Migration
  def self.up
    create_table "articles" do |t|
     t.string :content
     t.integer :post_id
     t.integer :user_id
    end

    add_index :articles, :post_id
    add_index :articles, :user_id
  end

  def self.down
    drop_table "articles"
  end
end


In some cases we may not sure which column need to index, In that case I strongly recommend to use the below gem which creates indexes for all the foreign_key columns on fly by running a simple command.

gem rails_indexes(A rake task to track down missing database indexes.) https://github.com/umamahesh/rails_indexes

usage:

add the gem to your gem file.

gem "rails_indexes"

> bundle install

> rake db:index_migration

Display a migration for adding/removing all necessary indexes based on associations:


Thank You,
Uma Mahesh.

postgres_ext gem native postgres datatypes in rails

Hi,

We are using different datatypes depending on the requirement of data that is being saved in database.

Coming to postgres database, it supports many datatypes and we can't use all those datatypes directly in rails.

To over come this problem, we had a new gem postgres_ext


postgres_ext supports 3.2 and above version of rails.

postgres_ext adds migration and schema.rb support for the following PostgresSQL type:

    INET
    CIDR
    MACADDR
    UUID
    Arrays


example:

create_table :users do |t|
  t.inet :myip


  t.cidr :mysubip


  t.macaddr :ip_address


  t.uuid :member_id

  t.integer :friend_ids, :array => true

end


postgres_ext converts INET and CIDR values to IPAddr instances.

example:

create_table :inet_examples do |t|
  t.inet :ip_address
end

class MyExample < ActiveRecord::Base
end

my_example = MyExample.new
my_example.ip_address = '127.0.0.0/34'
my_example.ip_address
# => #<IPAddr: IPv4:127.0.0.0/255.255.255.0>
my_example.save

my_example_u = MyExample.first
my_example_u.ip_address

# => #<IPAddr: IPv4:127.0.0.0/255.255.255.0>



array type:

example;

create_table :people do |t|
  t.integer :favorite_numbers, :array => true
end

class User < ActiveRecord::Base
end

user = User.new
user.like_numbers = [1,2,3]
user.flike_numbers
# => [1,2,3]
user.save

user_2 = user.first
user_2.like_numbers
# => [1,2,3]
user_2.like_numbers.first.class
# => Fixnum



here is the git url: https://github.com/umamahesh/pg_array_parser

Thank You,
Uma Mahesh.

Thursday, 28 June 2012

minimum things that a rails developer should have


This what I think a middle rails developer should have :


1. 2-3 RoR projects experience long enough to find design flaw in the implementation
2. Fluent with one test engine (test-unit, rspec, minitest)
3. Knowing minimal gems (authentication, pagination)
4. Writing idiomatic ruby code
5. Knowing at least 1 thing he doesn't like in RoR

The above points are said by martin one of my linkedin friend.

Thank You,
Uma Mahesh.

David Heinemeier Hansson rails creator

David Heinemeier Hansson rails creator

From Wikipedia, the free encyclopedia

David Heinemeier Hansson (known to the Ruby and ALMS communities as DHH) is a Danish programmer and the creator of the popular Ruby on Rails web development framework and the Instiki wiki. He is also a partner at the web-based software development firm 37signals.

Hansson co-wrote Agile Web Development with Rails with Dave Thomas in 2005 as part of The Facets of Ruby Series. He also co-wrote and Getting Real and Rework with Jason Fried.

Programming
In 1999 Hansson founded and built a Danish online gaming news website and community called Daily Rush, which he ran until 2001..

After attracting the attention of Jason Fried by offering him help with PHP coding, Hansson was hired by Fried to build a web-based project management tool, which ultimately became 37signals' Basecamp Software as a Service product.

To aid the development process, Hansson used the then-obscure Ruby programming language to develop a custom web framework. The web framework he created was later released separately from the project management tool as the open source project Ruby On Rails. In 2005 he was recognized by Google and O'Reilly with the Hacker of the Year award for his creation of Ruby on Rails.


facebook page : http://www.facebook.com/pages/David-Heinemeier-Hansson

official website : http://david.heinemeierhansson.com/

twitter page : https://twitter.com/#!/dhh


Thank You,
Uma Mahesh.

famous people saying about rails

Yukihiro Matsumoto, Creator of Ruby
“Rails is the killer app for Ruby.”

Evan Williams founder of Twitter
“After researching the market, Ruby on Rails stood out as the best choice. We have been very happy with that decision. We will continue building on Rails and consider it a key business advantage.”

Tim O'Reilly, Founder of O'Reilly Media
“Ruby on Rails is a breakthrough in lowering the barriers of entry to programming. Powerful web applications that formerly might have taken weeks or months to develop can be produced in a matter of days.”

James Duncan Davidson, Creator of Tomcat and Ant
“Rails is the most well thought-out web development framework I’ve ever used. And that’s in a decade of doing web applications for a living. I’ve built my own frameworks, helped develop the Servlet API, and have created more than a few web servers from scratch. Nobody has done it like this before.”

Thank You,
Uma Mahesh.

Friday, 22 June 2012

ssh connection refused error

Hi,

I am trying to connect remote system with ssh and I have faced below issue. 

ssh umamahesh@123.45.67.891

ssh: connect to host 123.45.67.891 port 22: Connection refused


Here are few things that need to check while connecting to remote system.

1. Does remote system has ssh enables ?

run the below command
> 'sudo /etc/init.d/ssh start'

Here is the error that may rise when there was no ssh installed

sudo: /etc/init.d/ssh: command not found

so you need to install ssh first in your remote system to accept the ssh connections.

run the below command to install ssh

> sudo apt-get install openssh-server openssh-client

Now try the below command

> sudo /etc/init.d/ssh start

Now it should work as ssh installed.

ThanK you,
Uma Mahesh.

Wednesday, 20 June 2012

difference between update_attribute and update_attributes

Hi,

update_attribute and update_attributes both are used to update the object without having explicitly Active Record to update.

The main difference between them is update_attribute will not call validations and callbacks, it will skip validations and callbacks while updating the record.

Let me explain clearly

      # File vendor/rails/activerecord/lib/active_record/base.rb, line 2614
       def update_attribute(name, value)
         send(name.to_s + '=', value)
         save(false)
       end


In the above parameter that was passing for the save is false. i.e  save(perform_validation = false)). So it will skip the validations and there by callbacks.


Coming to update_attributes

      # File vendor/rails/activerecord/lib/active_record/base.rb, line 2621
       def update_attributes(attributes)
         self.attributes = attributes
         save
       end

In the above case we are not passing any parameter for the save. So it will be default 'true'. So it will call the validations.

Important point at here is, we should choose update_attribute mainly for updating Boolean column datatypes, where it is not required that much validations.

Here is the syntax that should be used for update_attribute

@user.update_attribute(:status, true)

The main goal of update_attribute is to bypass the stuff and make the update operation fast.




Thank You,
Uma Mahesh.