[Ruby] refactoring/drying help

John Barnette jbarnette at gmail.com
Thu Oct 11 14:04:41 PDT 2007


On 10/11/07, shane becker <veganstraightedge at gmail.com> wrote:
> sooo... how could i do this more dry?

This is untested, but something similar should work. Caveats: The
split is wrong, I assume that your list of roles is comprehensive (and
named the same as the columns), and I removed the full_name column
(which shouldn't be in the DB).


~ j.


roles = { :writer => 1, :artist => 2 }

Artist.find(:all).each do |artist|
  first_name, last_name = artist.first_name_last_name.split
  role_id = roles[roles.keys.detect { |r| artist.send(r) }]

  Person.create!(
    :first_name => first_name,
    :last_name => last_name,
    :role_id => role_id)
end


More information about the Ruby mailing list