Permalink
Browse files

A Post Can Have Many Comments

  • Loading branch information...
1 parent 1f4250f commit 856f45c8770c5d5f82fccae27119641615e09ec8 @xmonkey committed Nov 28, 2012
@@ -1,5 +0,0 @@
-class PostController < ApplicationController
- def index
- @posts = Post.all
- end
-end
@@ -0,0 +1,5 @@
+class PostsController < ApplicationController
+ def index
+ @posts = Post.all
+ end
+end
View
@@ -0,0 +1,4 @@
+class Comment < ActiveRecord::Base
+ belongs_to :post
+ attr_accessible :content
+end
View
@@ -1,3 +1,4 @@
class Post < ActiveRecord::Base
+ has_many :comments
attr_accessible :title, :url
end
@@ -1,4 +0,0 @@
-<h2>Post list</h2>
-<% @posts.each do |post| %>
- <p> <%= link_to post.title, post.url %></p>
-<% end %>
@@ -0,0 +1,4 @@
+<h2>Post list</h2>
+<% @posts.each do |post| %>
+ <p> <%= link_to post.title, post.url %> <%= post.comments.count.to_s + " comments" %> </p>
+<% end %>
View
@@ -1,4 +1,4 @@
CourseProject::Application.routes.draw do
#root to: "main#index"
- root to: "post#index"
+ root to: "posts#index"
end
@@ -0,0 +1,11 @@
+class CreateComments < ActiveRecord::Migration
+ def change
+ create_table :comments do |t|
+ t.text :content
+ t.references :post
+
+ t.timestamps
+ end
+ add_index :comments, :post_id
+ end
+end
View
@@ -11,7 +11,16 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20121127074132) do
+ActiveRecord::Schema.define(:version => 20121128135451) do
+
+ create_table "comments", :force => true do |t|
+ t.text "content"
+ t.integer "post_id"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
+ add_index "comments", ["post_id"], :name => "index_comments_on_post_id"
create_table "posts", :force => true do |t|
t.string "url"
@@ -0,0 +1,9 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
+
+one:
+ content: MyText
+ post:
+
+two:
+ content: MyText
+ post:
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class CommentTest < ActiveSupport::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end

0 comments on commit 856f45c

Please sign in to comment.