Saturday, November 19, 2011

Gmail SMTP setting in rails3

SMTP setting for rails3and put new gmail.rb then run.

require 'rubygems'
require 'action_mailer'
require 'tlsmail'

  Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)
  ActionMailer::Base.delivery_method = :smtp
  ActionMailer::Base.perform_deliveries = true
  ActionMailer::Base.default_charset = "utf-8"
  ActionMailer::Base.raise_delivery_errors = true


ActionMailer::Base.smtp_settings = {
  :enable_starttls_auto => true,
  :address => "smtp.gmail.com",
  :port => 587,
  :domain => "domainname",
  :authentication => :plain,
  :user_name => "notifications@domainname",
  :password => "password"
}


class Emailer < ActionMailer::Base
  def test_email()
    subject    "Test message"
    from       "Test mail"
    recipients 'recipients@domainname.com'
    body        "Test mail for gmail SMTP setting"
  end
end

Emailer.deliver_test_email()

Friday, November 18, 2011

PostgreSQL row sort with NULL values and empty string at last

ORDER BY NULLIF(first_name, '') DESC NULLS LAST
More examples here

Ubuntu OS process state code


       Ubuntu OS  process state codes are

      1.  D   uninterruptible sleep (usually IO)
      2.  R   runnable (on run queue)
      3.  S   sleeping
      4.  T   traced or stopped
      5.  Z   Defunct ("zombie") process, terminated but not reaped by its parent
      6. X   dead (should never be seen)
      7. W  paging (not valid since the 2.6.xx kernel)

What is the performance difference distinct vs group by SQL command


Don't use distinct command.It performance way is slow. group by command performance very high compare to distinct command.Please see the real time difference in the below example query.

1) select count(*) from (select distinct contact_id from contacts_lists) a;

 count
 65474

 1 row(s)

 Total runtime: 40,800.186 ms

2) select count(*) from (select contact_id from contacts_lists group by contact_id) a;

 count
 65474

 1 row(s)

 Total runtime: 32,302.381 ms


Wednesday, November 16, 2011

PGError: ERROR: duplicate key value violates unique constraint


Syntax: 

SELECT setval('table_name_id_seq'::regclass, MAX(id)) FROM table_name;

Example of usage:

SELECT setval('personal_contacts_personal_folders_id_seq'::regclass, MAX(id)) FROM personal_contacts_personal_folders;



Tuesday, January 11, 2011

Eager Loading Associations in rails


Eager loading is the mechanism for loading the associated records of the objects returned by Model.find using as few queries as possible.
N + 1 queries problem
Consider the following code, which finds 10 clients and prints their postcodes:


clients = Client.all(:limit => 10)
clients.each do |client|
  puts client.address.postcode
end

This code looks fine at the first sight. But the problem lies within the total number of queries executed. The above code executes 1 ( to find 10 clients ) + 10 ( one per each client to load the address ) = 11 queries in total.


This is possible by specifying the includes method of the Model.find call
Revisiting the above case, we could rewrite Client.all to use eager load addresses:

clients = Client.includes(:address).limit(10)
clients.each do |client|
  puts client.address.postcode
end

The above code will execute just 2 queries, as opposed to 11 queries in the previous case:

SELECT * FROM clients LIMIT 10
SELECT addresses.* FROM addresses
  WHERE (addresses.client_id IN (1,2,3,4,5,6,7,8,9,10))