[Ruby] refactoring/drying help

shane becker veganstraightedge at gmail.com
Thu Oct 11 13:54:13 PDT 2007


what i'm doing is moving is an old crappy db setup to a new one that  
is (hopefully) less crappy. for example, the poorly named 'artists'  
table has a boolean columns for the different kinds of jobs: writer,  
artist, editor etc. each person can have any number of those columns  
set to true. so it's possible for artist.artist = false and  
artist.writer = true. stupid.

i'm walking through and saving them out as individual records in my  
new people table. the way i'm doing it is obviously not DRY. i  
couldn't figure out to do it any other way.

sooo... how could i do this more dry?

artists:
- first_name_last_name :string
- writer :boolean
- artist :boolean

people:
- first_name :string
- last_name :string
- full_name :string
- rold_id :integer

roles:
1: writer
2: artist


Artist.find(:all).each do |artist|
   people = []

   if artist.writer then
     person = Person.new
     person.full_name = artist.first_name_last_name
     person.first_name = person.full_name.split.first
     person.last_name = person.full_name.split.last
     person.role_id = 1
     people << person
   end

   if artist.artist then
     person = Person.new
     person.full_name = artist.first_name_last_name
     person.first_name = person.full_name.split.first
     person.last_name = person.full_name.split.last
     person.role_id = 2
     people << person
   end

   people.each { |person| person.save! }
end


kthx


More information about the Ruby mailing list