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 :D --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 %> --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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...
-----Original Message-----
From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
[mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of
Marra
Sent: Tuesday, June 24, 2008 9:52 AM
To: Ruby on Rails: Talk
Subject: [Rails] 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 :D
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Thanks guys! I tried your way and it worked :D You guys ROCK. On Jun 24, 1:19 pm, Thorsten Müller <thors...-1oxKqHKwyltBDgjK7y7TUQ@public.gmane.org> wrote:> 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 %>--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---