I am building out a messaging system for my app that is almost exactly how Stackoverflow works. I have two loops running. One for each Post and then inside, one for Comments made for that Post. Right now; it displays All the comments in the entire database and not just for those Posts.
discussion.html.erb
<% @projects.posts.each do |post| %>
<%= post.content %>
</div>
<% @projects.posts.comments.each do |comment| %>
<%= comment.content %>
</div>
<% end %>
<% end %>
projects_controller.rb
def discussion
@projects = Project.includes(:posts => :comments).find(params[:id])
@posts = Project.find(params[:id]).posts
@comments = Project.find(params[:id]).posts.comments
end
1 Answer