# How do I use an instance of another Controller as service object?

**URL:** <https://www.ruby-forum.com/t/how-do-i-use-an-instance-of-another-controller-as-service-object/258901>\
**Category:** Uncategorized\
**Created:** [March 17, 2020, 7:41am UTC](https://www.ruby-forum.com/t/how-do-i-use-an-instance-of-another-controller-as-service-object/258901 "2020-03-17T07:41:18Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![von\_spotz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e19adc/32.png) [@von\_spotz](https://www.ruby-forum.com/u/von_spotz)\
**Post date:** [March 17, 2020, 7:41am UTC](https://www.ruby-forum.com/t/how-do-i-use-an-instance-of-another-controller-as-service-object/258901/1 "2020-03-17T07:41:18Z")

</div>

Hello !

How do I use an instance of another controller as service Object?

Personally, my problem-case is that I have a ActsAsPolymorphicTree Concern with a polymorphic join table, such that db\_records of different types can be parent and children of each other.

The join table model “TreeLeafs” has a controller TreeLeafsController and the route takes the type and id of a parent and a child db\_record.

So I have a multiplexer that is supposed to do what I ask for in the title.

I want the multiplexer to get the controller/record\_type and action from the route and create an instance of the suchlike controller in order to render or invoke actions like “new” when I need a new instance variable of that kind.

it looks something like this then

controller\_name = “#{resource\_type.camelize.pluralize}”+“Controller”  
controller\_klass = Object.const\_get(controller\_name)  
controller = controller\_klass.new  
request.filtered\_parameters[:controller] = controller\_name  
controller.params = params  
controller.request = request  
controller.response = response  
controller.send sym\_controller\_action

What would be the correct way to retrieve a newly created instance from this controller-service-object for instance? Can I somehow extract it from the request ?

Also it doesn’t work to call `render` on the controller instance. The view doesn’t have the instance variables created in the action at its disposal.

Thanks
