How to configure Rails App to use Mongodb

In this post i will show you guys how to make your rails application to talk with Mongodb.

Mongodb is a nosql database which stores data as document.

Inorder to configure your rails app with you need to follow the following steps:

Step 1 :

create a rails application as

$rails new my_app –skip-active-record   or     $ rails new my_app  -O -T

in the above step we skip Active Record which is the default ORM layer in Rails.

 

STEP 2:

Now enter the following gems in your gem file

  1. gem ‘mongoid’, ‘2.0.0.beta.19’
  2. gem ‘bson_ext’

$ bundle install

STEP 3 :

Now you successfully installed all the gems required by the app to interact with the mongodb database.

$ rails  g  mongoid:config

it will create configuration file in  config/mongoid.yml

STEP 4 :

generate a model to test your app whether it is connected to mongodb or not

$ rails g model article name:string content:text

and go to your model file and see that it looks like this

class Article  
  include Mongoid::Document  
  field :name, :type => String  
  field :content, :type => String  
end 

 

so thats it you configured your app with mongodb.


 

 

 

 

 

Ruby Specific Questions

The best place for learning ruby is to get started with the programming-ruby. It fairly covers the important bits in a very readable language. Here are a few quick questions on ruby:

  1. What is rubygems?
  2. What is a Symbol?
  3. What is the difference between a Symbol and String?
  4. What is the purpose of yield?
  5. How do you define class variables?
  6. How do you define instance variables?
  7. How do you define global variables?
  8. How can you dynamically define a method body?
  9. What is a Range?
  10. How can you implement method overloading?
  11. What is the difference between ‘&&’ and ‘and’ operators?
  12. What is the convention for using ‘!’ at the end of a method name?
  13. What is a module?
  14. What is mixin?
  15. How will you implement a singleton pattern?
  16. How will you implement a observer pattern?
  17. How can you define a constant?
  18. How can you define a custom Exception?
  19. How can you fire a method when a module is included inside a class?
  20. What is the default access modifier (public/protected/private) for a method?
  21. How can you call the base class method from inside of its overriden method?

ANSWERS:

1.What is rubygems?

Ans.      RubyGem is a software package, commonly called a “gem”. Gems contain a packaged Ruby application or library. The RubyGems software itself allows you to easily download, install, and manipulate gems on your system.

Each gem has a name, version, and platform. For example, the rake gem has a 0.8.7 version. Rake’s platform is ruby, which means it works on any platform Ruby runs on. Other platforms include java (like nokogiri) and mswin32 (like sqlite-ruby).

Gems can be used to extend or modify functionality within a Ruby application. Commonly, they’re used to split out reusable functionality that others can use in their applications as well. Some gems also provide command line utilities to help automate tasks and speed up your work. As of Ruby 1.9.2, RubyGems is included when you install the programming language, so gems are both ubiquitous and extremely useful. If you’re using an earlier version of Ruby, it’s simple to install RubyGems as an addon.

2.What is the difference between a Symbol and String?

Ans.    Symbol are same like string but both behaviors is different based on object_id, memory and process time (cpu time) Strings are mutable , Symbols are immutable.

Mutable objects can be changed after assignment while immutable objects can only be overwritten. For example

p "string object jak".object_id #=> 22956070
p "string object jak".object_id #=> 22956030
p "string object jak".object_id #=> 22956090

p :symbol_object_jak.object_id #=> 247378
p :symbol_object_jak.object_id #=> 247378
p :symbol_object_jak.object_id #=> 247378

p " string object jak ".to_sym.object_id #=> 247518
p " string object jak ".to_sym.object_id #=> 247518
p " string object jak ".to_sym.object_id #=> 247518

p :symbol_object_jak.to_s.object_id #=> 22704460
p :symbol_object_jak.to_s.object_id #=> 22687010
p :symbol_object_jak.to_s.object_id #=> 21141310

And also it will differ by process time

For example:

Testing two symbol values for equality (or non-equality) is faster than testing two string values for equality,

Note : Each unique string value has an associated symbol

3.What is the purpose of yield?

Ans:     You invoke a block by using the yield statement.

Let.s look at an example of the yield statement:

#!/usr/bin/ruby

def test
   puts "You are in the method"
   yield
   puts "You are again back to the method"
   yield
end
test {puts "You are in the block"}

This will produce following result:

You are in the method
You are in the block
You are again back to the method
You are in the block

You also can pass parameters with the yield statement. Here is an example:

#!/usr/bin/ruby

def test
   yield 5
   puts "You are in the method test"
   yield 100
end
test {|i| puts "You are in the block #{i}"}

This will produce following result:

You are in the block 5
You are in the method test
You are in the block 100

Here the yield statement is written followed by parameters. You can even pass more than one parameter. In the block, you place a variable between two vertical lines (||) to accept the parameters. Therefore, in the preceding code, the yield 5 statement passes the value 5 as a parameter to the test block.

4.How do you define class variables?

Ans:                Class variables begin with @@ and must be initialized before they can be used in method definitions.

Referencing an uninitialized class variable produces an error. Class variables are shared among descendants of the class or module in which the class variables are defined.

Overriding class variables produce warnings with the -w option.

Here is an example showing usage of class variable:

#!/usr/bin/ruby

class Customer
   @@no_of_customers=0
   def initialize(id, name, addr)
      @cust_id=id
      @cust_name=name
      @cust_addr=addr
   end
   def display_details()
      puts "Customer id #@cust_id"
      puts "Customer name #@cust_name"
      puts "Customer address #@cust_addr"
    end
    def total_no_of_customers()
       @@no_of_customers += 1
       puts "Total number of customers: #@@no_of_customers"
    end
end

# Create Objects
cust1=Customer.new("1", "John", "Wisdom Apartments, Ludhiya")
cust2=Customer.new("2", "Poul", "New Empire road, Khandala")

# Call Methods
cust1.total_no_of_customers()
cust2.total_no_of_customers()

Here @@no_of_customers is a class variable. This will produce following result:

Total number of customers: 1
Total number of customers: 2

5.How do you define instance variables?

Ans:             Instance variables begin with @. Uninitialized instance variables have the value nil and produce warnings with the -w option.

Here is an example showing usage of Instance Variables.

#!/usr/bin/ruby

class Customer
   def initialize(id, name, addr)
      @cust_id=id
      @cust_name=name
      @cust_addr=addr
   end
   def display_details()
      puts "Customer id #@cust_id"
      puts "Customer name #@cust_name"
      puts "Customer address #@cust_addr"
    end
end

# Create Objects
cust1=Customer.new("1", "John", "Wisdom Apartments, Ludhiya")
cust2=Customer.new("2", "Poul", "New Empire road, Khandala")

# Call Methods
cust1.display_details()
cust2.display_details()

Here @cust_id, @cust_name and @cust_addr are instance variables. This will produce following result:

Customer id 1
Customer name John
Customer address Wisdom Apartments, Ludhiya
Customer id 2
Customer name Poul
Customer address New Empire road, Khandala








6.How do you define global variables?
Ans:     Global variables begin with $. Uninitialized global variables have 
the value nil and produce warnings with the -w option.Assignment to global variables 
alters global status.It is not recommended to use global variables. They make programs cryptic.

Here is an example showing usage of global variable.

#!/usr/bin/ruby

$global_variable = 10
class Class1
  def print_global
     puts "Global variable in Class1 is #$global_variable"
  end
end
class Class2
  def print_global
     puts "Global variable in Class2 is #$global_variable"
  end
end

class1obj = Class1.new
class1obj.print_global
class2obj = Class2.new
class2obj.print_global

Here $global_variable is a global variable. This will produce following result:

NOTE: In Ruby you CAN access value of any variable or constant by putting a hash (#) character just before that variable or constant.

Global variable in Class1 is 10
Global variable in Class2 is 10


8.What is a Range?
Ans:

2012 COAL MINE SCAM

 The CAG is at it again. About 16 months after it rocked the UPA government with its explosive report on allocation of 2G spectrum and licences, the Comptroller & Auditor General’s draft report titled ‘Performance Audit Of Coal Block Allocations’ says the government has extended “undue benefits”, totalling a mind-boggling Rs 10.67 lakh crore, to commercial entities by giving them 155 coal acreages without auction between 2004 and 2009. The beneficiaries include some 100 private companies, as well as some public sector units, in industries such as power, steel and cement. Continue reading

6 million LinkedIn passwords hacked

Encrypted passwords from the website posted online.

Linkedin hacked. Photograph, Getty Images.
LinkedIn is looking in to claims that the passwords of more than six million users have been leaked to a Russian website.
A file containing the encrypted passwords was put online, according to claims by security analysts. It was accompanied by an invitation to the hacking community to help with decrypting them.

Millions of LinkedIn passwords reportedly leaked online

 

A hacker says he’s posted 6.5 million LinkedIn passwords on the Web — hot on the heels of security researchers’ warnings about privacy issues with LinkedIn’s iOS app.

LinkedIn users could be facing yet another security problem.

A user in a Russian forum says that he has hacked and uploaded almost 6.5 million LinkedIn passwords, according to The Verge. Though his claim has yet to be confirmed, Twitter users are already reporting that they’ve found their hashed LinkedIn passwords on the list, security expert Per Thorsheim said. Continue reading

LinkedIn passwords leaked by hackers

LinkedIn homepageThe site had earlier issued a change to its mobile apps after a privacy flaw was uncovered

Social networking website LinkedIn has said some of its members’ passwords have been “compromised” after reports that over six million’ passwords had been leaked onto the internet.

Hackers posted a file containing encrypted passwords onto a Russian web forum.

They have invited the hacking community to help with decryption.

LinkedIn, which has over 150 million users, said the leaked passwords would no longer be valid. Continue reading

Google unveils new mapping technologies

Google hand demoAn Android smartphone showcasing the new 3D imagery technology

Google has demonstrated new mapping technologies in an effort to reassert its position as a market leader.While it boasts one billion users, Google Maps has recently seen defections by some key developers and partners.Reports suggest Apple may abandon Google Maps next week at its annual developer conference.They suggest Apple may announce its own mapping application to replace Google Maps on its smartphones and tablets.

To counteract any negative publicity, Google executives held a media event on Wednesday in San Francisco to preview new mapping features and trumpet a decade of achievements in digital mapping, including its use of satellite, aerial and street-level views. Continue reading