# Show Latest Notes Question

**URL:** <https://www.ruby-forum.com/t/show-latest-notes-question/141331>\
**Category:** Rails\
**Created:** [June 24, 2008, 6:54pm UTC](https://www.ruby-forum.com/t/show-latest-notes-question/141331 "2008-06-24T18:54:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f6c823/32.png) [@Marra](https://www.ruby-forum.com/u/Marra)\
**Post date:** [June 24, 2008, 6:54pm UTC](https://www.ruby-forum.com/t/show-latest-notes-question/141331/1 "2008-06-24T18:54:52Z")

</div>

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 😃

---

<div class="post-metadata">

**Author:** ![Marra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f6c823/32.png) [@Marra](https://www.ruby-forum.com/u/Marra)\
**Post date:** [June 24, 2008, 7:19pm UTC](https://www.ruby-forum.com/t/show-latest-notes-question/141331/2 "2008-06-24T19:19:29Z")

</div>

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 %\>

---

<div class="post-metadata">

**Author:** ![Marra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f6c823/32.png) [@Marra](https://www.ruby-forum.com/u/Marra)\
**Post date:** [June 24, 2008, 7:37pm UTC](https://www.ruby-forum.com/t/show-latest-notes-question/141331/3 "2008-06-24T19:37:40Z")

</div>

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| %\>

\<%= h note %\>

\<% end %\>

That’s air code, but I think something like that should work…

---

<div class="post-metadata">

**Author:** ![Marra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f6c823/32.png) [@Marra](https://www.ruby-forum.com/u/Marra)\
**Post date:** [June 24, 2008, 10:06pm UTC](https://www.ruby-forum.com/t/show-latest-notes-question/141331/4 "2008-06-24T22:06:31Z")

</div>

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