# Custom method in model to return an object

**URL:** <https://www.ruby-forum.com/t/custom-method-in-model-to-return-an-object/149792>\
**Category:** Rails\
**Created:** [September 29, 2008, 9:53pm UTC](https://www.ruby-forum.com/t/custom-method-in-model-to-return-an-object/149792 "2008-09-29T21:53:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![silviu.postavaru](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/58956e/32.png) [@silviu.postavaru](https://www.ruby-forum.com/u/silviu.postavaru)\
**Post date:** [September 29, 2008, 9:53pm UTC](https://www.ruby-forum.com/t/custom-method-in-model-to-return-an-object/149792/1 "2008-09-29T21:53:44Z")

</div>

Hi! I am new to rails(and ruby) and I have a C background, and I would  
like some help.

In the database I have a field named ‘body’ that has an XML in it. The  
method I created in the model looks like this:

def self.get\_personal\_data\_module(person\_id)  
person\_module = find\_by\_person\_id(person\_id)  
item\_module = Hpricot(person\_module.body)  
personal\_info = Array.new  
personal\_info = {:studies =\> (item\_module/“studies”).inner\_html,  
:birth\_place =\> (item\_module/“birth\_place”).inner\_html,  
:marrital\_status =\> (item\_module/“marrital\_status”).inner\_html}  
return personal\_info  
end

I want the function to return an object instead of an array. So I can  
use Module.studies instead of Model[:studies].

Thank you for your time.  
Silviu

---

<div class="post-metadata">

**Author:** ![silviu.postavaru](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/58956e/32.png) [@silviu.postavaru](https://www.ruby-forum.com/u/silviu.postavaru)\
**Post date:** [September 30, 2008, 4:16am UTC](https://www.ruby-forum.com/t/custom-method-in-model-to-return-an-object/149792/2 "2008-09-30T04:16:16Z")

</div>

> I want the function to return an object instead of an array. So I can  
> use Module.studies instead of Model[:studies].

you can create a new class and return it:

class PersonalInfo  
attr\_accessor :studies, :birth\_place, :marital\_status  
end

def self.get\_personal\_data\_module(person\_id)  
person\_module = find\_by\_person\_id(person\_id)  
item\_module = Hpricot(person\_module.body)  
personal\_info = PersonalInfo.new  
personal\_info.studies = (item\_module/“studies”).inner\_html  
personal\_info.birth\_place =  
(item\_module/“birth\_place”).inner\_html  
personal\_info.marrital\_status =  
(item\_module/“marrital\_status”).inner\_html}  
return personal\_info  
end
