Active Record associations performance

Hi,

I’m running into a memory performance issue with Active Record
associations.
I’m tinkering with Redmine, a rails-based project management
application. The central model in the application is ‘Project’ which
associates via has_many to model Issue. One of the things you can do
with Projects is copy them. In a nutshell, copy is implemented like
this (in class Project):
def copy(other_project)
other_project.issues.each do |other_issue|
self.issues << other_issue.copy
end
self.save
end

The problem is that issues are themselves complex objects, and for a
project with many issues (several thousand), self.issues grows to the
point of exhausting memory and swap on the box, effectively making a DOS
attack out of this function. So the question is, is there an idiomatic
Active Record solution to this problem? Perhaps committing and flushing
the association list every N adds?

Thanks in advance for any advice.

Anton Ivanov wrote:

Hi,

this (in class Project):
def copy(other_project)
other_project.issues.each do |other_issue|
self.issues << other_issue.copy
end
self.save
end

Not tested… just thinking…

Get the link(associative) table that’s used in the many to many
association and then make sure it has a first class model sitting on top
of that so you can work with that exclusively…
Try:

def copy(other_project)
other_project.issues.each do |other_issue|
Issue_projects.new(:issue => other_issue.copy, :project =>
self).save();
end
end

I believe that the memory requirements will be lower as the self will
not be adding onto it’s current issues during the copy…

ilan