Home How to retrieve and output the last record by foreign id?
Reply: 1

How to retrieve and output the last record by foreign id?

Damon Delcoro
1#
Damon Delcoro Published in 2018-01-09 22:48:18Z

I have 2 Models in my application. Project & Note. A project has_many notes. This all works great but on the projects index page.html.erb I would like to show the last note for a project.

My question is how do I retrieve this and then display it, on the index screen?

Shiko
2#
Shiko Reply to 2018-01-09 23:10:00Z

You can get the last note using project.notes.last. In your view, add below :

<% projects.each do |project| %>
  <% last_note = project.notes.last %>
  <%= last_note.note unless last_note.nil? %> # get last note per project if it exists
<% end %>
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.328211 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO