# Best way to organize non-controller logic?

**URL:** <https://www.ruby-forum.com/t/best-way-to-organize-non-controller-logic/84407>\
**Category:** Rails\
**Created:** [March 21, 2006, 12:46am UTC](https://www.ruby-forum.com/t/best-way-to-organize-non-controller-logic/84407 "2006-03-21T00:46:21Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [March 21, 2006, 12:46am UTC](https://www.ruby-forum.com/t/best-way-to-organize-non-controller-logic/84407/1 "2006-03-21T00:46:21Z")

</div>

I don’t want to put certain code in my controller because I think it  
makes things more confusing.

What is the best way to manage business logic such that it doesn’t end  
up in the controller?

Basically, where should I put my regular utility classes and backing  
objects that may not be models?

Thanks,  
Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [March 21, 2006, 12:55am UTC](https://www.ruby-forum.com/t/best-way-to-organize-non-controller-logic/84407/2 "2006-03-21T00:55:06Z")

</div>

application.rb can be a good spot for general purpose stuff - I put  
e.g. my date formatting stuff in there to ensure dates are shown  
consistently across an entire site.

Regards

Dave M.

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [March 21, 2006, 1:32am UTC](https://www.ruby-forum.com/t/best-way-to-organize-non-controller-logic/84407/3 "2006-03-21T01:32:38Z")

</div>

you could create modules and put them in the lib directory.

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [March 21, 2006, 3:54am UTC](https://www.ruby-forum.com/t/best-way-to-organize-non-controller-logic/84407/4 "2006-03-21T03:54:47Z")

</div>

Can you give an example of business logic that doesn’t interact with a  
model?

On 3/20/06, Chris H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > removed\_email\_address@domain.invalid

–  
Jeremy H.

> **[Jeremy Huffman, the Website - jeremyhuffman.com](https://jeremyhuffman.com/)**
>
> Jeremy Huffman, the Website
>         
>       
>     
>     
> Getting in Touch
> 
> You can email me if you can guess the addres...

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [March 21, 2006, 1:03pm UTC](https://www.ruby-forum.com/t/best-way-to-organize-non-controller-logic/84407/5 "2006-03-21T13:03:16Z")

</div>

for me, I have to access external information on an AS/400 system. My  
only  
access to the system is via ODBC, for which there is no AR Adapter  
avaliable.

so what i did was create a module for making connections to and  
extracting  
information from the AS/400 and put it in the lib directory.

AS400.rb

require ‘dbi’  
module AS400  
def AS400.get\_some\_data(args)  
# do the AS/400 stuff here  
# massage the data if needed (strip whitespaces, do some  
calculations,  
etc)  
return data  
end  
end

then where i need it, typically a controller, i just require ‘AS400’ and  
do  
AS400.get\_some\_data(12345) .

Chris

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [March 21, 2006, 4:23pm UTC](https://www.ruby-forum.com/t/best-way-to-organize-non-controller-logic/84407/6 "2006-03-21T16:23:36Z")

</div>

Personally, I’d say that looks like a model. You can just put it in  
your models directory, and use it from controllers the way you  
normally would. You’ll need to set up some mocks in order to unit  
test it, since most back-end systems like that don’t provide usable  
test regions. (At least not for the meaning of the word that OO  
programmers are comfortable with.)

You can use the “model” and “service” controller class methods to make  
sure it gets ‘required’ in the appropriate place.

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [March 21, 2006, 8:01pm UTC](https://www.ruby-forum.com/t/best-way-to-organize-non-controller-logic/84407/7 "2006-03-21T20:01:16Z")

</div>

> you could create modules and put them in the lib directory.

It’s cleaner BUT it’s cached in development mode =\> if you change it,  
you need to restart the server.

Alain

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [March 21, 2006, 8:18pm UTC](https://www.ruby-forum.com/t/best-way-to-organize-non-controller-logic/84407/8 "2006-03-21T20:18:19Z")

</div>

I have a filename that is an attribute of one of my model objects. It  
references a file in the file system.

I need to figure out if that file is an Excel file, a CSV file, or  
neither.

Wes

Jeremy H. wrote:

> Can you give an example of business logic that doesn’t interact with a  
> model?
> 
> On 3/20/06, Chris H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > > removed\_email\_address@domain.invalid
> 
> –  
> Jeremy H.  
> [http://www.jeremyhuffman.com](http://www.jeremyhuffman.com)

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [March 28, 2006, 2:16pm UTC](https://www.ruby-forum.com/t/best-way-to-organize-non-controller-logic/84407/9 "2006-03-28T14:16:39Z")

</div>

class MyObject \< ActiveRecord::Base  
…  
validates\_each :filename do |fname|  
unless [".xls", “.csv”].include?(fname.extname)  
model.errors.add(fname, “File does not appear to be an excel or  
csv  
file”)  
end  
end
