Skip to content

Instantly share code, notes, and snippets.

@tdm00
Created October 4, 2012 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tdm00/3833847 to your computer and use it in GitHub Desktop.
Save tdm00/3833847 to your computer and use it in GitHub Desktop.
Dashboard of posts and comments
class Comment < ActiveRecord::Base
# Associations
belongs_to :post
belongs_to :commenter
end
class Commenter < ActiveRecord::Base
# Associations
has_many :comments
end
<% @posts.each do |post| %>
<%= post.title %> <br>
# what to loop over the comments here, there maybe more then one, and show the commenter name
# for example output:
# This is my first post
# - john said this is great
# - tom said i hope the tigers win
# This is my second post
# how do I do this?
- <%= post.comment.commenter.name %> said <%= post.comment.body %>
<% end %>
class MainController < ApplicationController
def main
@posts = Post.includes(:comments => {:commenter})
respond_to do |format|
format.html # dashboard.html.erb
format.json { render json: @requisitiontestreports }
end
end
end
class Post < ActiveRecord::Base
# Associations
has_many :comments
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment