# How to DRY up this model a bit?

**URL:** <https://www.ruby-forum.com/t/how-to-dry-up-this-model-a-bit/131754>\
**Category:** Rails\
**Created:** [March 13, 2008, 12:55am UTC](https://www.ruby-forum.com/t/how-to-dry-up-this-model-a-bit/131754 "2008-03-13T00:55:17Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![fizban](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f4b2a3/32.png) [@fizban](https://www.ruby-forum.com/u/fizban)\
**Post date:** [March 13, 2008, 12:55am UTC](https://www.ruby-forum.com/t/how-to-dry-up-this-model-a-bit/131754/1 "2008-03-13T00:55:17Z")

</div>

Hi again,

I have this model, trip.rb, which is getting bloated with the same  
things repeated all over. I’m new to both rails and ruby so I’m  
looking for help on how to DRY this thing a bit

I basically have to repeat this a lot of time (replacing “departure”  
and “departures” with other models) and I would love to find a way to  
write it once 🙂

def new\_departure\_attributes=(departure\_attributes)  
departure\_attributes.each do |attributes|  
departures.build(attributes)  
end  
end

def existing\_departure\_attributes=(departure\_attributes)  
departures.reject(&:new\_record?).each do |departure|  
attributes = departure\_attributes[departure.id.to\_s]  
if attributes  
departure.attributes = attributes  
else  
departures.delete(departure)  
end  
end  
end

def save\_departures  
departures.each do |departure|  
departure.save(false)  
end  
end

Can anyone educate me a bit? 🙂

Thanks
