Another newbie Ruby on Rails question:
In my post view, I want to show the authors name. The post database table stores the authors id which is the same as the users id column in the users table. The user also has a username column in the user table.
So having the users id, how do I get the users name?
User.find(user_id).username
is one way.
If you were to have a belongs_to relationship between post and user it would be
post.user.username
which is much more 'the Rails way'.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With