helper method placement advice

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

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.

2. 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.

Hi --

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?

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 :slight_smile:

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