Rails Newbie - very simple question

(Joe Zambella) #1

Hello, I have a simple question based around the following example:

I have two model classes: Book and Author. The books table in my database has an author_id, which links the book to the appropriate author (aside: I know this isn't the best way to implement this, seeing that a book can have multiple authors, but I'm trying to keep it simple for the example and my own learning ;-)).

Now, I have my show.rhtml file for my Book class, where I want to display all of the pertinent info: book title, date published, and, most importantly to this example, the author's name.

My question is: given the book's author_id, what is the preferred/recommended way to grab the auther's information from the database from my book's show page?

Do I need to add a method to my Author class which, when provided with the unique author_id, will return the necessary information?

Thanks!
Joe Z.

(daz) #2

Joe Zambella wrote:

Hello, I have a simple question [...]

Joe,

You'd be better helped, I think, by asking on
the Textdrive Rails forum:
http://forum.textdrive.com/viewforum.php?id=9

.... or on irc:
irc://irc.freenode.org/rubyonrails

But check back here in case someone gives a
more helpful reply than this one :wink:

daz

[Info plucked from:]

#3

My question is: given the book's author_id, what is the
preferred/recommended way to grab the auther's information from the
database from my book's show page?

# Try this:
Author.find(@book.author_id)

Now, this is such a basic question, I have to recommend some of the
following to get productive in Rails:
* find and go through a tutorial
* buy and go through Agile Web Development with Rails
* use the documentation at http://documentation.rubyonrails.com/ and
http://api.rubyonrails.com/

You can ask questions on the Rails mailing list
(http://lists.rubyonrails.org/mailman/listinfo/rails) or on IRC
(irc://irc.freenode.org/rubyonrails) as you learn.

(This isn't the most appropriate forum for Ruby on Rails specific
questions - you will get much better help from the resources I've
mentioned.)

Cheers,
Dave