Forum: Ruby on Rails controller subroutines

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-02-21 02:58
(Received via mailing list)
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
6ef8cb7cd7cd58077f0b57e4fa49a969?d=identicon&s=25 Brian Hogan (Guest)
on 2006-02-21 03:32
(Received via mailing list)
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
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-02-21 04:03
(Received via mailing list)
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
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-02-21 04:34
(Received via mailing list)
nevermind...I figured it out...

myfind = performfind
variable = result of method

doh - I am brain damaged...learned something new though

Thanks

Craig
This topic is locked and can not be replied to.