# Model

**URL:** <https://www.ruby-forum.com/t/model/130457>\
**Category:** Rails\
**Created:** [February 28, 2008, 1:00pm UTC](https://www.ruby-forum.com/t/model/130457 "2008-02-28T13:00:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [February 28, 2008, 1:00pm UTC](https://www.ruby-forum.com/t/model/130457/1 "2008-02-28T13:00:07Z")

</div>

I have two tables. They are Service Desk and Incident.

I have to relate one of my service desk records with one or more  
service  
desk record(s) and/or one or more incident record(s). Hence the result  
of  
my search will both service desk records and/or incident records.

MY PROBLEM IS WHAT SHOULD BE THE TABLE STRUCTURE TO STORE THIS RESULT  
INCLUDING THE TYPE OF IT(ie WHETHER IT IS SERVICE DESK OR INCIDENT  
RECORD)

Thanks  
Sijo

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [February 28, 2008, 6:26pm UTC](https://www.ruby-forum.com/t/model/130457/2 "2008-02-28T18:26:07Z")

</div>

One, you can do just as you say and “include the type”. Create a  
‘type’ column in each table with either an integer or you can place a  
string of “Service Desk” or “Incident”

Also what i would do is, I am positive you have a record or ticket  
number or something like that. I would place a ‘SD’ or ‘IN’ in front  
of the number to so you can tell what kind of record it is. If you  
have the same columns in each table then you can combine all the  
records in one table and just have a Ticket or Record table.

For example - SD1234 or IN4567

Hope this helps… JB

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [February 28, 2008, 6:38pm UTC](https://www.ruby-forum.com/t/model/130457/3 "2008-02-28T18:38:03Z")

</div>

Use a polymorphic association. It’s polymorphic in the sense that it  
can associate to any other object in the system. You accomplish this  
by adding a compound foreign key – one column gives the name of the  
class and the other gives the id of the instance. Your models would  
look something like this:

class ServiceDesk  
has\_many :serviceables, :polymorphic=\>true  
belongs\_to :service\_desk, :as=\>:serviceable

…  
end

class Incident  
belongs\_to :service\_desk, :as=\>:serviceable

…  
end

With that…  
@service\_desk = ServiceDesk.find(1)  
@service\_desk.serviceables \<= a collection of ServiceDesk and  
Incident objects (actually a proxy…)

Your migration probably looks like this:

def self.up  
add\_column :service\_desks, :serviceable\_type, :string  
add\_column :service\_desks, :serviceable\_id, :integer  
end

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [February 29, 2008, 5:29am UTC](https://www.ruby-forum.com/t/model/130457/4 "2008-02-29T05:29:29Z")

</div>

Hi

Thanks for your reply

Sijo

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [February 29, 2008, 5:47am UTC](https://www.ruby-forum.com/t/model/130457/5 "2008-02-29T05:47:21Z")

</div>

Can u Please evaluate the following as a solution

public  
def get\_related\_record\_model  
“ServiceDesk”  
end

in the SericeDesk and

public  
def get\_related\_record\_model  
“Incident”  
end

in Incident model

# Then to store my result a table service\_desk\_related\_records

id  
service\_desk\_ticket\_id integer  
related\_record\_id integer  
related\_record\_model string #To get this I call  
get\_related\_record\_model  
related\_record\_association\_type\_id | integer

created…What do u think of this way?

Sijo

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [February 29, 2008, 5:21pm UTC](https://www.ruby-forum.com/t/model/130457/6 "2008-02-29T17:21:08Z")

</div>

That’s essentially what the polymorphic does for you, with the bonus  
that “rails magic” takes care of all the coding for you. You only  
have to change “related\_record\_model” to “related\_record\_type” and  
your work is done.

By the way, you don’t need a method to render out the name of the  
class. Use class.name
