# One model with two controller?

**URL:** <https://www.ruby-forum.com/t/one-model-with-two-controller/189082>\
**Category:** Rails\
**Created:** [May 24, 2010, 9:16am UTC](https://www.ruby-forum.com/t/one-model-with-two-controller/189082 "2010-05-24T09:16:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nirosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ce7236/32.png) [@nirosh](https://www.ruby-forum.com/u/nirosh)\
**Post date:** [May 24, 2010, 9:16am UTC](https://www.ruby-forum.com/t/one-model-with-two-controller/189082/1 "2010-05-24T09:16:06Z")

</div>

i have a comment Model. it has already “belongs\_to :poem” entry.  
is there any way to use this model to another controller (eg:-  
belongs\_to :post) . if it is possible then how do i do the migration  
and other stuffs please explain a bit.

nirosh

---

<div class="post-metadata">

**Author:** ![nirosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ce7236/32.png) [@nirosh](https://www.ruby-forum.com/u/nirosh)\
**Post date:** [May 24, 2010, 2:58pm UTC](https://www.ruby-forum.com/t/one-model-with-two-controller/189082/2 "2010-05-24T14:58:33Z")

</div>

I don’t think controllers have anything to do with your question. A  
controller is a program that receives requests from the browser and  
processes them, most likely involving models, and then sends a  
response back to the browser.

What you are talking about, I think, is about activerecord  
associations. You can make as many associations between models  
(tables) as you wish. A comment can belong to a poem and also to a  
post:

class Comment \< ActiveRecord::Base  
belongs\_to :poem  
belongs\_to :post  
end

class Poem \< ActiveRecord::Base  
has\_many :comments  
end

class Post \< ActiveRecord::Base  
has\_many :comments  
end

---

<div class="post-metadata">

**Author:** ![nirosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ce7236/32.png) [@nirosh](https://www.ruby-forum.com/u/nirosh)\
**Post date:** [May 24, 2010, 3:08pm UTC](https://www.ruby-forum.com/t/one-model-with-two-controller/189082/3 "2010-05-24T15:08:30Z")

</div>

Hi nirosh,

belongs\_to associations represent other models/tables not controllers.

You can have as many different Controllers as you like, created  
through migrations or manually - they are simply the way of ‘getting  
in’ to your application. All the heavy lifting should be done in the  
models.

You can set your models up as so:

comment  
has\_many: poems  
has\_many: posts

poem  
belongs\_to: comment

post  
belongs\_to: comment

Your poem and post tables would then each have a column/attribute:  
comment\_id

Each poem and each post could then ‘attach’ themselves to the same  
comment\_id so that each single comment could be associated with both a  
poem and a post.

You create your post migration the same way you created your poem  
migration, apart from any additional attributes specifically related  
to post.

HTH

Paul
