[Ruby] refactoring/drying help

Mike Emery philodoxx at gmail.com
Thu Oct 11 14:11:03 PDT 2007


I don't see why you need a full name attribute in the person table when you
already have a first name and a last name.

why not do something like:

Artist.find(:all).each do | artist |
  person = Person.new
  person.first_name = artist.full_name_last_name.split.first
  person.last_name = artist.full_name_last_name.split.last

  if artist.writer
    person.role_id = 1
  elsif artist.artist
    person.role_id = 2
  end

  person.save!
end


On 10/11/07, shane becker <veganstraightedge at gmail.com> wrote:
>
> 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
> _______________________________________________
> Ruby at zenspider.com - Seattle.rb non-commercial list
> http://www.zenspider.com/seattle.rb
> http://www.zenspider.com/mailman/listinfo/ruby
>



-- 
-Mike


More information about the Ruby mailing list