Recipes/categories to books/authors but listing doesn't work

People,

I have gone through the cookbook example:

and everything worked fine an a FC4/Postgres setup.

I then tried to reproduce this success with something that would be
useful to me - a library db using books & authors tables. The author
list works but NOT the book list (unless I take line #17 out).

I get this message:

Showing app/views/book/list.rhtml where line #17 raised:

You have a nil object when you didn’t expect it!
The error occured while evaluating nil.last_name

Extracted source (around line #17):

14: <% @books.each do |book| %>
15:
16: <%= link_to book.title, :action => “show”, :id => book.id
%>
17: <%= book.author.last_name %>
18:
19: <% end %>
20:

but as far as I can tell I have done the right things in the
controllers, models and views:

author_controller.rb:

@authors = Author.find  :all,
                        :order => 'last_name, first_name'

book_controller.rb:

@books = Book.find  :all,
                    :order => 'title, back'

author.rb:

has_many :books

book.rb:

belongs_to :author

book/list.rhtml:

<% @books.each do |book| %>

<% end %>

Book

Author

<%= link_to book.title, :action => "show", :id => book.id %> <%= book.author.last_name %>

authors table:

id | integer | not null default
nextval(‘public.authors_id_seq’::text)
last_name | character varying(20) |
first_name | character varying(20) |
other_name | character varying(20) |

books table:

id | integer | not null default
nextval(‘public.books_id_seq’::text)
media | character(2) | not null
title | character varying(50) | not null
year | integer | not null
cat | character(1) | not null
back | character(1) |
own | character(1) |
author_id | integer

I have been battling with this for ages but can’t see any bugs left -
any suggestions?

Thanks,

Phil.


Philip R.

Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275)
GPO Box 3411
Sydney NSW 2001
Australia
Mobile: +61:(0)411-185-652
Fax: +61:(0)2-8221-9599
E-mail: [email protected]

Philip R. <phil@…> writes:

I have been battling with this for ages but can’t see any bugs left -
any suggestions?

It would appear that you have a book with no author.

replace the stuff in your view with

<%=book.inspect%>

and see if any book has a nil author.

-damon