# Two instances of a controller?

**URL:** <https://www.ruby-forum.com/t/two-instances-of-a-controller/166672>\
**Category:** Rails\
**Created:** [April 28, 2009, 1:13pm UTC](https://www.ruby-forum.com/t/two-instances-of-a-controller/166672 "2009-04-28T13:13:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cojones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@cojones](https://www.ruby-forum.com/u/cojones)\
**Post date:** [April 28, 2009, 1:13pm UTC](https://www.ruby-forum.com/t/two-instances-of-a-controller/166672/1 "2009-04-28T13:13:47Z")

</div>

Hello,

I have written a guestbook and I’d like to use it twice in my  
application. One public guestbook and one private guestbook. How can I  
use the very same controller for two different guestbooks?

Thanks

---

<div class="post-metadata">

**Author:** ![cojones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@cojones](https://www.ruby-forum.com/u/cojones)\
**Post date:** [April 28, 2009, 1:32pm UTC](https://www.ruby-forum.com/t/two-instances-of-a-controller/166672/2 "2009-04-28T13:32:59Z")

</div>

I guess for the private guess book one needs to login. Then you’ll  
select the guess book for the logged in user. In the public guess  
book, one just enters their information and all entries can be saved  
to a default user. Well, that’s all there is too it.

Good luck,

-Conrad

Sent from my iPhone

On Apr 28, 2009, at 4:13 AM, Heinz S.  
\<removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![cojones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@cojones](https://www.ruby-forum.com/u/cojones)\
**Post date:** [April 28, 2009, 1:48pm UTC](https://www.ruby-forum.com/t/two-instances-of-a-controller/166672/3 "2009-04-28T13:48:49Z")

</div>

Yes of course, that’s how I wanna do it but the question was how to  
‘create’ these two different guestbooks. I have one controller,  
therefore I have one guestbook but how can I instantiate a second one  
with the same controller but different entries etc.?

Conrad T. wrote:

> I guess for the private guess book one needs to login. Then you’ll  
> select the guess book for the logged in user. In the public guess  
> book, one just enters their information and all entries can be saved  
> to a default user. Well, that’s all there is too it.
> 
> Good luck,
> 
> -Conrad
> 
> Sent from my iPhone
> 
> On Apr 28, 2009, at 4:13 AM, Heinz S.  
> \<removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![cojones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@cojones](https://www.ruby-forum.com/u/cojones)\
**Post date:** [April 28, 2009, 6:13pm UTC](https://www.ruby-forum.com/t/two-instances-of-a-controller/166672/4 "2009-04-28T18:13:32Z")

</div>

Not necessarily two tables. I read somewhere that it’s possible to use  
the same controller/model/whatever for various instances.

I don’t know if you guys got me right… I’ve got a guestbook  
controller/model/etc. and decide to have another guestbook. Copying the  
controller/model/etc. and rename it to guestbook2 is very stupid that’s  
why I would like to know how I can use the same controller/model/etc.  
and have 2 different guestbooks.

Colin L. wrote:

> Data is not stored in a controller it is stored in the database, using a  
> model. Is it that you want two tables with the two guestbooks in it?  
> That  
> does not sound like the best solution.
> 
> 2009/4/28 Heinz S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![cojones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@cojones](https://www.ruby-forum.com/u/cojones)\
**Post date:** [April 28, 2009, 3:18pm UTC](https://www.ruby-forum.com/t/two-instances-of-a-controller/166672/5 "2009-04-28T15:18:04Z")

</div>

Data is not stored in a controller it is stored in the database, using a  
model. Is it that you want two tables with the two guestbooks in it?  
That  
does not sound like the best solution.

2009/4/28 Heinz S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![cojones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@cojones](https://www.ruby-forum.com/u/cojones)\
**Post date:** [April 28, 2009, 6:47pm UTC](https://www.ruby-forum.com/t/two-instances-of-a-controller/166672/6 "2009-04-28T18:47:57Z")

</div>

I still don’t understand what you mean by two guestbooks. If all you  
want  
is for different users to see different data then have one guestbook but  
select the records based on the user. If you want the second book to be  
selected by a different url you could just pass a parameter to the  
controller (in the url) to select the records. There are probably  
numerous  
ways, it all depends what exactly you are trying to achieve.

2009/4/28 Heinz S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![cojones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@cojones](https://www.ruby-forum.com/u/cojones)\
**Post date:** [April 28, 2009, 7:02pm UTC](https://www.ruby-forum.com/t/two-instances-of-a-controller/166672/7 "2009-04-28T19:02:02Z")

</div>

Or perhaps what you want is a table guest\_books with one record for each  
book. Then the data is in a table guest\_book\_entries where GuestBook  
has\_many GuestBookEntries and GuestBookEntries belongs\_to GuestBook.

2009/4/28 Colin L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
