# Helper method placement advice

**URL:** <https://www.ruby-forum.com/t/helper-method-placement-advice/135987>\
**Category:** Rails\
**Created:** [April 27, 2008, 5:37pm UTC](https://www.ruby-forum.com/t/helper-method-placement-advice/135987 "2008-04-27T17:37:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hendrata](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/4bbf92/32.png) [@hendrata](https://www.ruby-forum.com/u/hendrata)\
**Post date:** [April 27, 2008, 5:37pm UTC](https://www.ruby-forum.com/t/helper-method-placement-advice/135987/1 "2008-04-27T17:37:37Z")

</div>

Hello,

I am developing a book-author database with RoR. The book-author  
relationship is many to many, therefore I created 3 tables as follows:

1. books
2. authors
3. writings, which has columns: id, book\_id, author\_id. This table  
describes the relationship between the books and the authors.

I have to create a lot of “helper” methods such as  
“find\_coauthors(author)”, or find\_books\_written\_by\_same\_authors, or  
find\_books\_written\_by\_coauthors.

My questions is, where is the best place to put these helpers?  
I have a few candidates:

1. helpers/book\_helper.rb -\> but I can’t access them from the  
controllers, can I?
2. book\_controllers.rb -\> but can I still access it from  
author\_controller.rb ? can controllers access methods in each other?
3. models/book.rb -\> but will I have access to the author model and  
writing model?

What is the standard practice in this case?

Thank you

---

<div class="post-metadata">

**Author:** ![hendrata](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/4bbf92/32.png) [@hendrata](https://www.ruby-forum.com/u/hendrata)\
**Post date:** [April 27, 2008, 5:49pm UTC](https://www.ruby-forum.com/t/helper-method-placement-advice/135987/2 "2008-04-27T17:49:54Z")

</div>

Hendrata Dharmawan wrote:

> I am developing a book-author database with RoR. The book-author  
> relationship is many to many, therefore I created 3 tables as follows:
> 
> 1. books
> 2. authors
> 3. writings, which has columns: id, book\_id, author\_id. This table  
> describes the relationship between the books and the authors.
> 
> I have to create a lot of “helper” methods such as  
> “find\_coauthors(author)”, or find\_books\_written\_by\_same\_authors, or  
> find\_books\_written\_by\_coauthors.

Always program the way you would state a problem out loud. When you say  
“we need  
to find books written by the same authors”, your sentence itself tells  
how to  
arrange things. Books.find\_by\_same\_author(anAuthor). Your finders should  
always  
belong to the object that

> 1. helpers/book\_helper.rb -\> but I can’t access them from the  
> controllers, can I?

Your model relationships belong only in the model.

> 1. book\_controllers.rb -\> but can I still access it from  
> author\_controller.rb ? can controllers access methods in each other?

class Book may freely reference class Author in its methods.

> What is the standard practice in this case?

Writing unit tests on the model will help reveal these relationships.

–  
Phlip

---

<div class="post-metadata">

**Author:** ![hendrata](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/4bbf92/32.png) [@hendrata](https://www.ruby-forum.com/u/hendrata)\
**Post date:** [April 27, 2008, 6:00pm UTC](https://www.ruby-forum.com/t/helper-method-placement-advice/135987/3 "2008-04-27T18:00:35Z")

</div>

Hi –

On Sun, 27 Apr 2008, Hendrata Dharmawan wrote:

> I have to create a lot of “helper” methods such as  
> writing model?
> 
> What is the standard practice in this case?

All of these methods belong in your model files.  
find\_coauthors(author) actually sounds like it should be a method on  
Author objects (dickens.co\_authors), which you could probably engineer  
as an association:

class Author \< AR::Base  
has\_many :co\_authors, # stuff here I’m too lazy to figure out 🙂

find\_books\_written\_by\_same\_authors sounds like a class method:

def self.find\_books…

in which you would do some kind of grouping of books based on their  
authors.

In general, intelligence about the “things” (books, authors) in your  
domain should reside with the things themselves (and thus be defined  
in the model files), while knowledge of the runtime request/response  
cycle belongs in the controller.

David

–  
Rails training from David A. Black and Ruby Power and Light:  
INTRO TO RAILS June 9-12 Berlin  
ADVANCING WITH RAILS June 16-19 Berlin  
INTRO TO RAILS June 24-27 London (Skills Matter)  
See [http://www.rubypal.com](http://www.rubypal.com) for details and updates!
