71bc5fc6f37ff015781a4a8b4cc20106

This is my first post. I'm moving data from one set of collections with join tables to a new set. I'm not sure if there's a better way, but here's my first take at it. i'm checking for duplicates as well.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
def move_old_collection_to_new_collection
  
  @old_collection = CollectionOne.find(params[:collectionone_id])
  @new_collection = CollectionTwo.find(params[:collectiontwo_id])

  record_types_of_join_tables = ['organization', 'event', 'program', 'person', 'project']
  
  #ex. moving CollectionOneEvent join records to CollectionTwoEvent and so on
  
  for rt in record_types_of_join_tables
    collection_record_type = eval "CollectionOne#{rt.capitalize}.find(:all, :conditions => ['collection_id = ?', #{@old_collection.id}])"
    unless collection_record_type.empty?
      for c in collection_record_type
        record_id = eval "c." + "#{rt}" + "_id"
        collection2_record_type = eval "CollectionTwo#{rt.capitalize}.find(:all, :conditions => ['collection_id = ? AND #{rt}_id = ?', #{@new_collection.id}, #{record_id}])"
        if collection2_record_type.empty?
          move_collection_record_to_new_collection = eval "CollectionTwo#{rt.capitalize}.new(:collection_id => #{@new_collection.id}, " + ":" + "#{rt}_id => #{record_id})"
          if move_collection_record_to_new_collection.save!
            puts "Logging results here"
          end
        end
      end
    end
  end
  
end

Refactorings

No refactoring yet !

A8d3f35baafdaea851914b17dae9e1fc

Adam

September 24, 2009, September 24, 2009 21:19, permalink

No rating. Login to rate!

I think you may want to rethink what you are doing completely. But in keeping with what you already have, I think this accomplishes the same. Assuming you have the appropriate associations defined on the Collection(One|Two) classes, of course.

1
2
3
4
5
6
7
8
9
10
def move_old_collection_to_new_collection
  source = CollectionOne.find(params[:collectionone_id])
  destination = CollectionTwo.find(params[:collectiontwo_id])

  %w(organizations events programs people projects).each do |association|
    source.send(association).each do |object|
      destination.send(association).create!(object.attributes)
    end
  end
end

Your refactoring





Format Copy from initial code

or Cancel