# Should the polymorfic modeling be used?

**URL:** <https://www.ruby-forum.com/t/should-the-polymorfic-modeling-be-used/158535>\
**Category:** Rails\
**Created:** [January 24, 2009, 1:26pm UTC](https://www.ruby-forum.com/t/should-the-polymorfic-modeling-be-used/158535 "2009-01-24T13:26:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![NycS\_Petr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/7cd45c/32.png) [@NycS\_Petr](https://www.ruby-forum.com/u/NycS_Petr)\
**Post date:** [January 24, 2009, 1:26pm UTC](https://www.ruby-forum.com/t/should-the-polymorfic-modeling-be-used/158535/1 "2009-01-24T13:26:31Z")

</div>

Hi experts,

I am new to rails and am struggling for several days with a following  
model  
(Rails 2.2.2).

People are submitting requests, each request has one submitter, one  
approver, several reviewers and several responsible people.

I was able to get this model working:

class Person \< ActiveRecord::Base  
has\_many :Requests  
end

class Request \< ActiveRecord::Base  
belongs\_to :Approver, :class\_name =\> “Person”, :foreign\_key =\>  
“approver\_id”  
belongs\_to :Submitter, :class\_name =\> “Person”, :foreign\_key =\>  
“submitter\_id”  
end

But I am really stuck in creating HABTM relationship between  
requests\<-\>reviewers and requests\<-\>responsible\_people… I do not want  
to  
create separate tables for each group of people as one is sometimes  
submitter/approver and sometimes belongs to reviewers/responsible.

Can you someone point me the right direction?  
Thanks!  
JetPac

---

<div class="post-metadata">

**Author:** ![NycS\_Petr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/7cd45c/32.png) [@NycS\_Petr](https://www.ruby-forum.com/u/NycS_Petr)\
**Post date:** [January 24, 2009, 3:15pm UTC](https://www.ruby-forum.com/t/should-the-polymorfic-modeling-be-used/158535/2 "2009-01-24T15:15:02Z")

</div>

Here’s how your code should be:

class Person \< ActiveRecord::Base  
has\_many :requests\_as\_approver, :class\_name =\> ‘Request’,  
:foreign\_key =\> ‘approver\_id’  
has\_many :requests\_as\_submitter, :class\_name =\> ‘Request’,  
:foreign\_key =\> ‘approver\_id’  
end

class Request \< ActiveRecord::Base  
belongs\_to :approver, :class\_name =\> “Person”, :foreign\_key =\>  
“approver\_id”  
belongs\_to :submitter, :class\_name =\> “Person”, :foreign\_key =\>  
“submitter\_id”  
end

I can’t see the need for a N:N association here (and thus as  
has\_and\_belongs\_to\_many association).

- 

Maurício Linhares  
[http://alinhavado.wordpress.com/](http://alinhavado.wordpress.com/) (pt-br) | [http://blog.codevader.com/](http://blog.codevader.com/)  
(en)
