#22 new
James B. Byrne

Missing/Empty? import/postgresql.rb

Reported by James B. Byrne | May 12th, 2009 @ 02:40 PM

I am just trying out ar-extensions (0.9.1) for the first time and when I do this:


    require 'ar-extensions/adapters/postgresql'
    require 'ar-extensions/import/postgresql'
...
      if new_rates.size % batch_size == 0 
        CurrencyExchangeRate.import new_rates, 
          :validate => validate
        new_rates = [] # empty the rates array
      end
...

Then I see this:

undefined method import' for #<Class:0x2b2e4c6dd850> (NoMethodError)

If I look at gems/ar-extensions-0.9.1/lib/ar-extensions/import/postgresql,rb I see an empty file:

$ ll /usr/lib64/ruby/gems/1.8/gems/ar-extensions-0.9.1/lib/ar-extensions/import/* ... -rw-r--r-- 1 root root 0 May 11 09:43 /usr/lib64/ruby/gems/1.8/gems/ar-extensions-0.9.1/lib/ar-extensions/import/postgresql.rb ...

Is this intended?

Comments and changes to this ticket

  • Zach Dennis

    Zach Dennis May 12th, 2009 @ 02:44 PM

    Hi James,

    I know this hasn't been implemented as I don't use PostgreSQL, but I am surprised by the error. It should fall back one insert per row. I'll look into this.

  • James B. Byrne

    James B. Byrne May 12th, 2009 @ 03:23 PM

    Maybe it is something that I am doing wrong in my call. I back dated to 0.8.2 and get the same result.

    I am trying to do this within an ActiverRecord model so perhaps that is the problem. The entire code looks like this:

    
    class CurrencyExchangeRate < ActiveRecord::Base
      # This is a class method not an instance.
      def self.load(rates,date=false)
        # date can be nil, false, true or a date
        date = Time.now.strftime("%Y-%m-%d") unless date  
          # not nil or false
        date = date unless date == true                   
          # not a date
        # otherwise we use the individual dates passed in the array
        
        # Now, require ar-extensions for mass update
        require 'ar-extensions/adapters/postgresql'
        require 'ar-extensions/import/postgresql'
    
        # Now, set the number of rates to update per batch
        batch_size = 100
    
        # Now, define the row update array
        new_rates = Array.new # could use [] but this is clearer
    
        rates.each do |rate|  # rate is a hash
          new_rates << CurrencyExchangeRate.new(rate)
          if new_rates.size % batch_size == 0 
            # if we have exactly batch_size
            CurrencyExchangeRate.import new_rates, 
              :validate => validate
            new_rates = [] # empty the rates array
          end
        end
        if new_rates.size > 0
          # load last batch regardless of size
          CurrencyExchangeRate.import new_rates, 
            :validate => validate
        end
        puts 'forex loader end'
      end
    
    

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

ActiveRecord::Extension (aka ar-extensions) is a plugin to extend and
enhance the functionality of ActiveRecord. It starts by adding better
find support for ActiveRecord. It then adds mass data import
capabilities which are highly efficient and lastly it supports to_csv
functionality.

It also introduces a cool concept of creating easily extendable pieces
of ActiveRecord functionality, so developers don't have to understand
ActiveRecord internals or have the fear of breaking ActiveRecord
itself.

People watching this ticket

You can update this ticket by sending an email to from your email client. (help)