Now, I am having trouble with displaying the query result on
authors\show.html
I am having problems referencing the returned object and getting the two
columns displayed. The error is: "You have a nil object when you didn't
expect it!"
I have this in my model:
class Author < ActiveRecord::Base
has_many :books
def author_age
@results = Author.find :all, :conditions => ["age = ?",
params[:authors]]
end
end
It is pointing to the line with the loop:
<% @results.each do |result|
So for one reason or another @results is nil
Most likely your query doesn't return any records.
(btw: you better name that @authors instead of @results for
readablity)
So what is in params[:authors] ?
Where does this param come from and does it contain an age?
After all, that's what you ask Rails to search for in
@results = Author.find :all, :conditions => ["age = ?", params
[:authors]]
So if for example params[:authors] would contain an authors id or
name (as the name suggests) then it wouldn't find anything.
You could easily debug this with something like:
@results = Author.find :all, :conditions => ["age = ?", params
[:authors]]
logger.info "RESULT: #{@results.size}"
Then you would find the number of found records in development.log
(Where btw you could see, which params where sent to your controller)