# Controller subroutines

**URL:** <https://www.ruby-forum.com/t/controller-subroutines/71128>\
**Category:** Rails\
**Created:** [February 21, 2006, 2:58am UTC](https://www.ruby-forum.com/t/controller-subroutines/71128 "2006-02-21T02:58:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 21, 2006, 2:58am UTC](https://www.ruby-forum.com/t/controller-subroutines/71128/1 "2006-02-21T02:58:09Z")

</div>

Trying to find this documented in the Agile book and don’t see an  
appropriate example so I am confused.

I have a method in my controller…

def performfind  
myfind = [“Select \* from clients where …”]  
even more…complicated find here  
end

and within the controller, I want to use the ‘myfind’ string

How do I get the string back from the method (which I gather is supposed  
to be in ‘private’ section)?

Craig

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 21, 2006, 3:32am UTC](https://www.ruby-forum.com/t/controller-subroutines/71128/2 "2006-02-21T03:32:11Z")

</div>

If I were you, I might think about making that a method on one of the  
models, either as an instance method or a class method (depending on  
your  
needs).

in the client model

def self.find\_special\_clients  
Client.find\_by\_sql("select \* from clients where blah = true and  
something in (‘this’, ‘that’)  
end

This keeps that sort of logic out of the controller. Then in the  
controller, you only need  
@results = Client.find\_special\_clients

Much cleaner, and also easier to write a unit test for that logic.

Don’t forget, since it’s a method, you could pass parameters into it as  
well

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 21, 2006, 4:03am UTC](https://www.ruby-forum.com/t/controller-subroutines/71128/3 "2006-02-21T04:03:07Z")

</div>

Makes sense but I have a lot of different sort\_by techniques that depend  
upon a session variable so I can’t simply put one find in my model.

What I am trying to accomplish is a very complicated ‘conditions’ string  
and use it in various ‘instance methods’ (thanks for the language).

Thus I have the logic to condense a very complicated find into a string,  
all I want is the methodology to get that string back to various methods  
in my controller.

Craig

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 21, 2006, 4:34am UTC](https://www.ruby-forum.com/t/controller-subroutines/71128/4 "2006-02-21T04:34:00Z")

</div>

nevermind…I figured it out…

myfind = performfind  
variable = result of method

doh - I am brain damaged…learned something new though

Thanks

Craig
