开发者

Show last record in db table Rails .last method

开发者 https://www.devze.com 2023-01-26 15:35 出处:网络
I have 2 tables with a relation between them: Users table: user_id, Blogs table: user_id, blog_content,

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消