Pages

Showing posts with label rails. Show all posts
Showing posts with label rails. Show all posts

Friday, May 14, 2010

ActiveRecord Bulk Insertion of data in rails

why we go for bulk insertion?

You take one scenario like there is a loop which insert a record each time it runs. For each and every time it will run a Insert SQL statement for creating the record. You no need to care if it is small number of data. You take millions of record then it will definitely get into performance degradation and slow up your application. So in this case you should have something for bulk insertion.

Solution for Bulk Insert:

When I am looking for bulk insertion of data with activerecord, I got an excellent and efficient way of doing bulk insertion.You may heard of Activerecord::Extensions which gives you the API for bulk insertion.

Step 1:

First install gem

sudo gem install ar-extensions

step 2:

Include this gem in your model

include ar-extensions

Step 3:

Usage : Model.import(columns,values,options)

:columns => array of column identifier
:values => array of values
:options => hash of options

If your model is employee.rb

columns = [emp_id, emp_name, emp_address, emp_dob]
values = [[1, abc, def, '2010-01-01'],[2, foss, texas, '2009-01-01'],[3, xyz, mno, '2008-01-01'],[4, sammy, NYC, '2007-01-01'],......... etc.] (you can add millions of record data)

Employee.import(columns, values)


options:

:validate => true|false default : true. if it is false then it won't use validations

:on_duplicate_key_update => Array|Hash – this tells import to use the associated Array or Hash to update rows where a duplicate key already exists. This is specific MySQL

:ignore => true|false – this tells import to issue a “INSERT IGNORE” when running each SQL statement. This is useful when you are importing mass amounts of records and you don’t care if MySQL finds duplicate entries.

While using this import API you can reduce the data insertion time. I was more surprised with speed when using import API. I hope you also get surprised by using bulk insert.

References - http://www.continuousthinking.com/are/import

Tuesday, October 6, 2009

Rails ActionMailer gmail Configuration using SMTP

If u have RAILS_VERSION >= 2.3.3 and RUBY VERSION >= 1.8.7

then you no need to install any tls plugin.... Just follow the below steps....


Step 1

You have to add the following Configuration settings in {RAILS_ROOT}/config/environment.rb for Gmail ..


config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "gmail.com",
:user_name => "YourmailID@gmail.com",
:password => "password123",
:authentication => :login,
:enable_starttls_auto => true #This line is must to ensure the tls for Gmail
}
config.action_mailer.default_content_type = "text/html"

Step2
Create mailer for your Project

#Type this in terminal
script/generator mailer
for eg.,

$ script/generator mailer Mailnotifier
exists app/models/
create app/views/mailnotifier
exists test/unit/
create test/fixtures/mailnotifier
create app/models/mailnotifier.rb
create test/unit/mailnotifier_test.rb


Step 3
Then you need to generate the mailer method for describing the mail content etc.,

$ script/generate mailer Mailnotifier notifier
exists app/models/
exists app/views/mailnotifier
exists test/unit/
exists test/fixtures/mailnotifier
overwrite app/models/mailnotifier.rb? (enter "h" for help) [Ynaqdh] Y
force app/models/mailnotifier.rb
overwrite test/unit/mailnotifier_test.rb? (enter "h" for help) [Ynaqdh] Y
force test/unit/mailnotifier_test.rb
create app/views/mailnotifier/notifier.erb
create test/fixtures/mailnotifier/notifier

Step 4
Then your models/Mailnotifier.rb looks like

#models/Mailnotifier.rb

class Mailnotifier < ActionMailer::Base
def notifier(sent_at = Time.now)
subject 'Notifier'
recipients 'mailertest@gmail.com' # Give the recipients mailID
from 'somemail@gmail.com'
sent_on sent_at
body :greetings => 'Hi,'
end
end

Step 5
In views/mailnotifier/notifier.erb file you can format the mail content as you like....

You Type this in your Terminal change directory to your rails application

$ script/console
Loading development environment (Rails 2.3.4)
>> Mailnotifier.deliver_notifier


This will send You the mail to your recipients address... :)

It Works Cool for me.....

Saturday, September 19, 2009