Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

ActiveRecord::Base.each instance method

#215

[PATCH] These patch add: ActiveRecord::Base.each ( User.each {|u| u.name} ) ActiveRecord::Base.map ActiveRecord::Base.collect

But with the diference with User.all.each that just one user is fetch from the database at a time. An simple and efficient way to don't load all the database in memmory.

With migrations between two or more databases, is common to do something like

User.find(:all).each {|u| ... } that will load all the records in memmory.

Ruby-proff says that managing that big array is not the better way.

With these patch you can do something like these.

User.each :conditions => ["users.created_at > ?", Time.now-5.months] do |u| NewUser.schema.create({ :name => u.name.Capitalize :email => validate_email u.email ... }) end

These patch pass all test, and have news ones.

Any comment are wellcome.

Reported by Guillermo Álvarez · May 17th, 2008 @ 08:37 PM

State: resolved
Milestone: none
Assigned to: Jeremy Kemper Jeremy Kemper
Importance: Low

Activity

  1. wildchild
    wildchild

    My 5 cents for you (if i right understand your approach):

    ActiveRecord::Base.each.fetch(10) # fetch 10 records instead 1 (for small rows in example)

    Maybe use will_paginate there?

    May 17th, 2008 @ 09:07 PM

  2. Guillermo Álvarez
    Guillermo Álvarez

    ActiveRecord::Base.each

    ActiveRecord::Base.map

    ActiveRecord::Base.collect

    With migrations between two or more databases, is common to do something like

    User.find(:all).each {|u| ... } that will load all the records in memmory.

    Ruby-proff says that managing that big array is not the better way.

    For my personal projects I use something like these:

    http://github.com/guillermo/acti...

    User.each :conditions => ["users.created_at > ?", Time.now-5.months] do |u|

    NewUser.schema.create({

    :name => u.name.Capitalize

    :email => validate_email u.email

    ... })

    end

    If someone think it could be interesting, i can try to make a patch.

    Thanks

    May 17th, 2008 @ 08:15 PM

  3. Guillermo Álvarez
    Guillermo Álvarez

    My situation is these:

    desc 'Copy Posts from the phpbB3 table to the new forum'

    task :copy_posts => :environment do

    BbTopic.find(:all).each do |bb|

    f = Post.create({

    :title => conv.iconv(bb.topic_title),

    :user_id => bb.topic_poster,

    :forum_id= bb.forum_id,

    ...})

    end

    end

    If i run that i will get a huge array in memory that i doesn't want, so i change

    BbTopic.find(:all).each do |bb|

    to

    BbTopic.each do |bb|

    that just load one record a time.

    May 17th, 2008 @ 08:24 PM

  4. Tarmo Tänav
  5. wildchild
    wildchild

    Any progress with ticket?

    May 29th, 2008 @ 12:22 AM

  6. josh
    josh
    • State changed from new to stale
    • Tag set to enhancement

    August 29th, 2008 @ 04:41 PM

  7. Guillermo Álvarez
    Guillermo Álvarez
    • Tag changed from enhancement to activerecord, enhancement

    Hi.

    There is a patch to do for example Post.each.

    Please giveme comments since is my first patch to rails.

    September 9th, 2008 @ 01:37 PM

  8. Guillermo Álvarez
  9. Guillermo Álvarez
    Guillermo Álvarez
    • Title changed from ActiveRecord::Base.each instance method to [PATCH] ActiveRecord::Base.each instance method

    September 10th, 2008 @ 09:24 AM

  10. Pratik
    Pratik
    • Title changed from [PATCH] ActiveRecord::Base.each instance method to ActiveRecord::Base.each instance method
    • Tag changed from activerecord, enhancement to activerecord, enhancement, patch

    September 10th, 2008 @ 10:30 AM

  11. Jeremy Kemper
    Jeremy Kemper
    • State changed from stale to open
    • Milestone cleared.

    September 10th, 2008 @ 06:12 PM

  12. josh
    josh
    • Assigned user changed from josh to Jeremy Kemper

    September 10th, 2008 @ 06:30 PM

  13. Pratik
    Pratik
    • Milestone set to 2.x

    October 17th, 2008 @ 05:13 PM

  14. José Valim
    José Valim

    Hi Guillermo Álvarez,

    Would not be nice if your patch takes into account the option :limit and :interval?

    For example:

    @@@ruby Account.each(:interval => 10) do |account|

    # ...
    
    

    end

    
    
    This would query the database using 10 in 10 objects.
    
    The limit would be used if I don't want to go through all the database:
    
    @@@ruby
      Account.each(:interval => 10, :limit => 100) do |account|
        # ...
      end
    

    I have an implementation that I use on my projects (it's not totally abstracted as in your patches), but if it helps:

    @@@ruby def slow_each(options = {})

    total = options.delete(:limit) || self.count(:conditions => options[:conditions])
    options[:limit] = options.delete(:interval) || 1000
    offset = 0
    
    while total - offset > 0
      options[:offset] = offset
    
      self.find(:all, options).each do |record|
        yield(record)
      end
    
      offset += options[:limit]
    end
    
    

    end

    
    
    For fast each, we can use:
    
    @@@ruby
      def fast_each(options = {})
        total = options.delete(:limit) || self.last.id
        default_conditions = options.delete(:conditions)
        options[:limit] = options.delete(:interval) || 1000
        offset = 0
    
        while total - offset > 0
          options[:conditions] = self.__send__(:merge_conditions, default_conditions, ['id BETWEEN ? AND ?', offset + 1 , offset + options[:limit]])
    
          self.find(:all, options).each do |record|
            yield(record)
          end
    
          offset += options[:limit]
        end
      end
    

    October 19th, 2008 @ 11:36 AM

  15. Jeremy Kemper
    Jeremy Kemper
    • State changed from open to resolved
    • Milestone cleared.

    February 23rd, 2009 @ 06:51 PM

  16. Ryan Bigg
    Ryan Bigg
    • Tag cleared.
    • Importance changed from to Low

    Automatic cleanup of spam.

    October 9th, 2010 @ 10:01 PM