Show last record in db table Rails .last method
I have 2 tables with a relation between them:
Users table: user_id,
Blogs table: user_id, blog_content,
Im working with a view that should sho开发者_Go百科w a users latest blog entry.. When I use
<%= @users.blogs.last %>
I get "#"
Can someone assist as to why its showing "#" and how to actually show the the last blog entry made by a @user?
Many thanks!!
I found out, I needed to define what part of the hash I needed to show
<%= @users.blogs.last[:blog_content] %>
I'd have thought you want to do:
<%= @users.blogs.last.blog_content %>
What you've done will try and display the Blog object itself with the to_s
method, which returns something that looks like the following:
#<Blog:0x?????????>
Which on screen will just look like a hash.
精彩评论