# DRY AR models

**URL:** <https://www.ruby-forum.com/t/dry-ar-models/87946>\
**Category:** Rails\
**Created:** [March 7, 2007, 12:53am UTC](https://www.ruby-forum.com/t/dry-ar-models/87946 "2007-03-07T00:53:52Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pierre-Alexandre\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c4cdca/32.png) [@Pierre-Alexandre\_M](https://www.ruby-forum.com/u/Pierre-Alexandre_M)\
**Post date:** [March 7, 2007, 12:53am UTC](https://www.ruby-forum.com/t/dry-ar-models/87946/1 "2007-03-07T00:53:52Z")

</div>

Hey,

I need an advice 😉

Assuming you have a form like:  
Father’s kids: \<%= in\_place\_editor\_field %\>  
Mother’s kids: \<%= in\_place\_editor\_field %\>

with just a Person model.

You want your customers to write directly:  
Father’s kids: mike,dave,chad  
Mother’s kids: david

I think a proper method to do this is:  
1/ Create Father \< Person and Mother \< Person models  
2/ Create in those models accessors:  
def kids=(value)  
value.split(‘,’).each do |kid|  
child = Person.find\_by\_name(kid)  
child.nil? Person.create(:name =\> kid, :parent\_id =\> self.id) :  
child.parent\_id = self.id  
end  
self.save  
end

def kids  
# find etc.  
end  
3/ Simply put @father\_kids & @mother\_kids in your controller

But that’s not DRY at all. So to improve this code, we could simply put  
in a ‘lib/people.rb’ file:  
@people = [‘father’,‘mother’]  
@people.each do |parent|

# define\_method(“#{kids=}”) etc.

end

and require the file in our models.  
Actually, it’s what I have done in my project, and it works (hope you  
don’t find this ugly :)).

My question is: that not DRY enough. I mean, if you want to add an:  
Sister’s kids: etc.  
You’ll need to generate a model, require the same files etc.

So, is it ‘recommended’ (‘authorized’, is it proper code?) to add  
Classes on-the-file to AR? I mean, not to  
write Father.rb, Mother.rb but just create a load\_classes.rb (required  
in boot.rb I suppose) which define\_class (oops,  
[http://www.ruby-doc.org/core/](http://www.ruby-doc.org/core/) returns me a define\_class (WIN32COMGen)\<–  
?!) after AR has reconized the Person model.

Actually, you can do it with a rake file but you need to regenerate it  
whenever you change your @people array.

Thanks for your replies.

–  
,========================.  
| Pierre-Alexandre M. |  
| email : removed\_email\_address@domain.invalid |  
`========================’
