Thursday 19 December 2013

Build a hash from an array


 We need to build a hash, and we need to build it from each element in an array.

Old way

  
def visitors_name_email
  result = {}
  visitors.each do |visitor|
    result[visitor.name] = visitor.email
  end
  result
end

New way


def visitors_name_email
  visitors.inject({}) do |result, visitor|
    result.merge(visitor.name => visitor.email)
  end
end

Monday 16 December 2013

Saturday 9 November 2013

Difference b/w library and framework


Libraries just fit into your existing architecture and add a specific functionality whereas a Framework gives you an architecture and you will need to follow the rules. 

To make it simpler for you – Backbone and Knockout are JavaScript libraries and Ember and AngularJS are frameworks.

SET up Ruby on Rails on Ubuntu 13.10

Sunday 20 October 2013

New finding / Rails

Rails runner


 runs Ruby code in the context of Rails non-interactively.

eg.   Rails runner 'puts Account.some_method_name'   or

       Rails r 'puts Account.some_other_method'
 
 

How  to check for database name from Rails console ?


eg.   ModelName.connection.current_database

       Account.connection.current_database

Difference b/w :dependent => :destroy, :dependent => :delete


  :dependent => :destroy
     When a parent is deleted, ActiveRecord load every dependent record and calls a destroy on it.

  :dependent => :delete
     is called ActiveRecord issues a single SQL delete statement for the parent record's children.

 

How to restrict from generating default routes



1.  resources  :accounts, only: [ ]

2. resources  :accounts, only: :none

and in case you would like to have one action(buy) then
   resources :accounts, only: [:buy]



check for keyCode && which & keyup || keydown

$(document).keydown(function(e){
   var code = e.keyCode ? e.keyCode : e.which;
   alert(code);
});



Replace single quote with backslash single quote


puts "Cote d'Ivoir".gsub("'", %q(\\\')) #=> Cote d\'Ivoir

How to make entire td a link?



   td a {
    display: block;
    width: 100%;
    height: 100%;
  }



Tuesday 18 June 2013

Rails update



Cup.update_all({:broken_handle => true}, {:broken_handle => false})



The first argument are the attributes to update and the second is the filter conditions for the query.


Batches in Rails



a.  find_each
b. find_in_batches

 1.  It's use is intended for batch processing of large amounts of records that wouldn't fit in memory all at once.

2. If we just want to loop over less than 1000 records(which is default batch size), it's good to use the find methods.

3. We can control the starting point for the batch processing by supplying the :start option. This is useful if we want multiple workers dealing with the same processing queue. Say for example if we want worker_1 to handle records b/w 0 to 5000 and worker_2 from 5000 to 10000 by setting :start option on that worker.



Thursday 13 June 2013

Write whatever you feel like

I came across this blog today(13 June 2013) and had the similar kind of writings which I generally think and got my answers to most of it.

http://excid3.com/blog/3-rules-for-becoming-a-better-developer-in-2013/#.UboE-IoW3Zg

Saturday 1 June 2013

Browser

1. When  and what kind of data should be stored in browser.
1. How to store data on browser.

Sunday 26 May 2013

Quit Early, Quit Often

http://www.youtube.com/watch?v=D73mm29XXAw&t=0s

Today 25th of May 2013 was a sad day in life due to some personal reason but as I was going through my mail Inbox, I came across a video where Prof Deepak Malhotra whom i came to know only after watching the video.

I just loved the things which he told.

oh no, I have turned out to be a fan of him.

Friday 10 May 2013

Oh there are some things which you have promised to do.

Rails convention and tunning the application



http://opensoul.org/blog/archives/2012/05/23/why-our-code-smells/

http://adamsanderson.github.io/railsconf_2013/?full#26

http://blog.plataformatec.com.br/2011/02/understanding-the-latest-rails-benchmarks/

https://github.com/railsbp/rails_best_practices



PostgreSql


http://adamsanderson.github.io/railsconf_2013/?full#26

Sunday 5 May 2013

If you have any doubts over why we are doing something, you should ask someone immediately, or write them down to investigate later


Q:  Full text search

PostgreSQL
Elasticsearch
Sphinx - fully developed
Solr   - fully developed


How does rails index works ?


Facts:

map outputs an array with the ability to do some computational on it.

Facts:

user = User.where(:name => 'tapan')  --> It builds sql query
user.first --> Execute the query in other words it now talks with databases.

NameError
NoMethodError

Curiosity:  Just know commented on this video, waiting for approval

http://www.youtube.com/watch?v=mBXGBbEbXZY

Q. Why do we use symbol ?

 
bihar = {state: 'bihar', capital: 'patna'}

then, india.keys.map(&:object_id) => [163848, 3119208] (some arbitrary ids)

karnataka = {state: 'karnataka', capital: 'bangalore'}

india.keys.map(&:object_id) => [163848, 3119208] (some arbitrary ids)

Here it returns the same object id for keys(state, capital).

So it references the same symbol and thus saves memory.

Q. Why  does Symbol.all_symbols.include? :hello  

       always return true

   => It happens as because it will go and first check in the array whether this particular symbols exists ? and it happens to be present there then returns true : else it will be pushed to the array and then return true

Concatenation

1. puts "tapan" + "sharma"
2. puts "tapan".concat("sharma")
3. puts "tapan" "sharma"

Chaining if's

   
  puts "hello boy" if 1 == 1 if 2 == 2

Ruby zip method


order = %w{1 2 3 4}
animals = %w{ant bat cat dog elephant}
p order.zip(animals)
=> [["1", "ant"], ["2", "bat"], ["3", "cat"], ["4", "dog"]]
p Hash[order.zip(animals)]
=> {"1"=>"ant", "2"=>"bat", "3"=>"cat", "4"=>"dog"}

Array creation

a = [*1..8]

Json


 require 'json'

h = {a: 'apple', b: 'boy', c: 'cat'}
j h 
 => {"a":"apple","b":"boy","c":"cat"}

jj h  
=> {
  "a": "apple",
  "b": "boy",
  "c": "cat"
}

__method__ :

  check for method name.

source_location :

  this locates the method.

Difference between scopes and class method

http://blog.plataformatec.com.br/2013/02/active-record-scopes-vs-class-methods/

Method Look up in Ruby.

Very well explained and written.

http://blog.jcoglan.com/2013/05/08/how-ruby-method-dispatch-works/comment-page-1/#comment-11917"


StringInquirer is a subclass of String


Command to create dump and restore


  pg_dump dbname > outfile

 psql dbname < infile

 Take dump of some tables in postgresql

    pg_dump --table=tutor_invoices --table=students veritas_development > out.sql