Show Latest Notes Question

I'm working on this application where the user can add new notes to a specific article. I want to display the last note on the add new notes page for that article.

I have a problem that if you try to visit the add a new note page for an article that doesn't have any notes, it will give you an error saying the first column of the notes table is nil - if I swap around the sql it would change the error to cannot find id. I've tried so many things but none of it work and I really would appreciate any help.

Basically if their is a note for an article I want it to show up above the form to add new notes to an article - if their isn't any notes for that article then just show the form.

Thanks for any help :smiley:

I guess you have a setup like this:

Articles model: has_many :notes

Notes model: belongs_to :article

Then in the controller (assuming you have an article in @article): @note = @article.notes.find(:first, :order => "created_at DESC") will give you the latest added note (if any)

in the view: <% if @note then %> ... show the note... <% end %>

Maybe something like:

# notes_controller.rb def new   where_stuff = ["article_id = ?", params[:article_id] ]   @last_notes = Note.find(:all, :conditions => where_stuff, :limit => 1, :order => 'created_at DESC') end

# notes/new.html.erb <% @last_notes.each do |note| %>   <p>     <%= h note %>   </p> <% end %>

That's air code, but I think something like that should work...

Thanks guys! I tried your way and it worked :smiley: You guys ROCK.