Why can't I access this field? (Rails 3)
I really can't figure out why I can't access the user_id field for the class Post. I have it as a field in my database (I can see it, and it isn't blank. Yet for some reason, I'm getting this error:
undefined method `user_id' for #<Class:0x103497448>
Extracted source (around line #10):
7: <h2>Topics</h2>
8: <% @board.topics.each do |topic| %>
9: <% @post = topic.posts(1) %>
10: <b><%= User.find(@post.user_id).username %> says <%= link_to topic.subject, [@board, top开发者_开发百科ic] %></b><br />
11: <% end %>
12: <% end %>
13:
topic.posts(1)
will return an array. Hence #<Class:0x103497448>
The right way to get the first post for a topic would be
9: <% @post = topic.posts.first %>
10: <b><%= User.find(@post.user_id).username %> says <%= link_to topic.subject, [@board, topic] %></b><br />
Try <% @post = topic.posts.limit(1) %>
精彩评论