This may seem like a real simple question, but do associations work both
I have a user model with, has_many :posts
I have a post model with, belongs_to :user
My posts controller has
@posts = Post.all
and my view does a '@posts.each do |post|' loop
In that loop, can I do:
<td><% post.user %></td>
...to give me back the user that made that post?
In the rails console I can do user.posts (that gives me all the posts a
user has made) but I can't do post.user (to give me the user).
I can do post.user_id which gives me the user_id, but I want their name
not their user_id - perhaps with something like post.user.name
Am I doing something wrong? I thought one of the benefits of
associations like this was so we wouldn't have to do separate queries?
Sorry if this is a silly question - I'm a Rails nube.
Thanks in advance!