Saturday, November 17, 2012

Multiple ways to import your data in ror

1. Importing the csv data from a specific location through Rake Task
URL of that blog - http://rorguide.blogspot.in/2012/11/importing-csv-data-from-specific.html

2. Importing the csv data by browsing and uploading csv from application screen
URL of that blog - http://rorguide.blogspot.in/2012/11/importing-csv-data-by-browsing-and.html

Importing the csv data by browsing and uploading csv from application screen

Importing the csv data by browsing and uploading csv from application screen

Given below code will be placed in view which will allow user to browse file to upload from view. Notice the code
:html => { :multipart => true }
which is mandatory for uploading file.

<% form_for :dump, :url=>{:controller=>"imports", :action=>"csv_import_eqdata2"}, :html => { :multipart => true } do |f| -%>
 <table border=1>
   <tr>
     <td>
      <label for="dump_file">
        Select a EQ Data CSV File2 :
      </label>
     </td>
     <td >
       <%= f.file_field :file -%>
     </td>
   
     <td colspan='2'>
       <%= submit_tag 'Submit' -%>
     </td>
   </tr>
 </table>
<% end -%>

==================
After upload when user will hit submit button, it will call imports controller, csv_import_eqdata2 action. Here are the codes which will be written in controller:

def csv_import_eqdata2 
  @parsed_file=CSV::Reader.parse(params[:dump][:file])
  n=0
     
  @parsed_file.each  do |row|
       
     rsymbol = row[0].strip if !row[0].blank?
     rseries = row[1]
     rtottrdqty = row[2]
     rtottrdval = row[3]

     @eqrawdatas = Eqrawdata.find(:all, :conditions => ["symbol='#{rsymbol}' and series ='#{rseries}' and tottrdqty ='#{rtottrdqty}' and tottrdval ='#{rtottrdval}' and deleted_at is null"])
     
     presentinlotsize = Lotsizedata.find(:first, :conditions =>["symbol='#{rsymbol}' "])

     #Accept only those records whose series is EQ only
     if @eqrawdatas.blank? && !rsymbol.blank? && !rseries.blank? && (rseries == "symbol" or rseries == "EQ") && !presentinlotsize.blank?
     c=Eqrawdata.new
     c.symbol= rsymbol
     c.series=rseries
     c.tottrdqty = rtottrdqty
     c.tottrdval= rtottrdval

     if c.save
        n=n+1
        GC.start if n%50==0
     end
     flash.now[:message]="CSV Import Successful,  #{n} new records added to data base"
     end
     end
   return redirect_to(eqdatalist_url)
end

Importing the csv data from a specific location through Rake Task

1. Importing the csv data from a specific location through Rake Task:

We can import data from csv file which is placed in csv folder in our Rails application root directory. Here we checks that csv file name should start with eq2 and ends with .csv, only these csv files should be picked to parse. All such csv files will be parsed. Place the given below code in "lib/tasks" folder as a rake task and run to import data.

Here are code details:


namespace :import do

  desc 'import csv files of eq2'
  task :eq2 => :environment do
    root = Rails.root.to_s
    require 'csv'

    #importing eq2  csv files where name starting from eq2 and ending with csv
    eq_files = Dir.glob("#{root}/csv/eq2*.csv")
    puts "Found eq2 csv file named  #{eq_files}"

    if !eq_files.blank?
      eq_files.each do |eq_file|
        puts "eq1 file is not blank #{eq_file}"
       
        @parsed_file = CSV::Reader.parse(File.open(eq_file, "rb"))
        n = 0
        @parsed_file.each_with_index do |row, i|
          next if i == 0 || i == 1
          break if row[2].blank?

          rsymbol = row[2].strip if !row[0].blank?
          rseries = row[3]

          presentinmaster = Masterdata.find(:first, :conditions =>["nse_symbol='#{rsymbol}' "])
          
          #Accept only those records which fulfill following conditions
          if !presentinmaster.blank? !rsymbol.blank? && !rseries.blank? && (rseries == "symbol" or rseries == "EQ")
            c=Eqmasterdata.new
            c.symbol= rsymbol
            c.series=rseries
            c.masterdatas_id = presentinmaster.id
            if c.save
              puts "CSV Import Successful,  #{n} new records added to data base"
              n=n+1
              GC.start if n%50==0
            else
              puts "EQ2 could not be save, error is #{c.errors}"
              #else writing error in different table
              AllError.create(:error_of => 'EQ2', :error_detail => c.errors, :error_summary => "Data not saved")
            end
          end

        end
      end
    end
  end
end

Friday, November 16, 2012

Multiple ways to export your data in ruby on rails

1. Prompting and letting user save csv in a particular folder:

Export excel from Rails application - User gets a link on view and when user clicks on that link a prompt comes asking user to save the exported excel on clients computer at desired location. In this case, you can create excel view and perform view formatting as we do with html page.

URL of blog - prompting-and-letting-user-save-csv-in-particular-folder


2. Saving the csv at a particular pre-defined folder:

Export csv at a particular pre-defined folder specified in rails application: User gets a link on view and when user clicks on that link user is not asked about the location for saving the csv, csv is exported and saved at pre-defined location. In this case, you cannot create excel view.

URL of blog - export-csv-at-particular-pre-defined-folder

CSV reader syntax change in Ruby 1.8.7 and Ruby 1.9.2

The way CSV file is opened in Ruby 1.8.7 and in Ruby 1.9.2 are different, here are the syntax:


@parsed_file = CSV::Reader.parse(File.open(restaurant_file, "rb"))
when running from ruby 1.8.7

new
@parsed_file = CSV.read(restaurant_file)
when running from ruby 1.9.2