Topic: DRY model

My Person model is the place where i do manage also the creation of all the info related to the person: Addresses, phone_numbers and so on.
For each person i can have any number of addresses, phone numbers et al.

So for each type of record related to the same Person i have this code:

# here I save all the addresses
@data[:address].each do |d|
  @evald  = d.reject {|i,f| f.empty?}
  self.addresses.create(d) unless @evald.empty?
  @evald = nil

# here I save all the phone numbers

@data[:phone_number].each do |d|
  @evald  = d.reject {|i,f| f.empty?}
  self.phone_numbers.create(d) unless @evald.empty?
  @evald = nil

How can this code be parametrized so that i can use only one line of code for each record type, something like this:

Re: DRY model

Something like:

def update_data(data, model)
  data.each do |d|
    self.send(model).create(d) unless (d.values - [""]).empty?

should do it.


Re: DRY model


once more I'm blinded by the elegance and power of Ruby....
...and by the skill level of people like you who takes the time to "enlighten" the masses...

The usage of "send" or things like (d.values - [""]).empty? are gems that leaves people like me (coming from Fortran, Pascal, C, PL/I, VB and so on) simply astonished.

One line of code instead of three: this is DRYing up code!!!!