diff --git a/app/models/comment.rb b/app/models/comment.rb index 0209dd9..d2adef4 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -5,7 +5,7 @@ class Comment < ActiveRecord::Base validates :user_id, :presence => true belongs_to :user - belongs_to :asciicast + belongs_to :asciicast, :counter_cache => true attr_accessible :body diff --git a/app/views/asciicasts/_preview.html.erb b/app/views/asciicasts/_preview.html.erb index e5fe85b..3153761 100644 --- a/app/views/asciicasts/_preview.html.erb +++ b/app/views/asciicasts/_preview.html.erb @@ -19,4 +19,8 @@ <% if asciicast.description.present? %>

<%= asciicast.description %>

<% end %> + + <% if asciicast.comments_count > 0 %> +

<%= link_to "#{asciicast.comments_count} comments", asciicast_path(asciicast, :anchor => 'comments') %>

+ <% end %> diff --git a/db/migrate/20120409142151_add_comments_count_to_asciicast.rb b/db/migrate/20120409142151_add_comments_count_to_asciicast.rb new file mode 100644 index 0000000..1bcb712 --- /dev/null +++ b/db/migrate/20120409142151_add_comments_count_to_asciicast.rb @@ -0,0 +1,5 @@ +class AddCommentsCountToAsciicast < ActiveRecord::Migration + def change + add_column :asciicasts, :comments_count, :integer, :null => false, :default => 0 + end +end diff --git a/db/schema.rb b/db/schema.rb index 28b549c..737b87b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20120406152447) do +ActiveRecord::Schema.define(:version => 20120409142151) do create_table "asciicasts", :force => true do |t| t.integer "user_id" @@ -35,6 +35,7 @@ ActiveRecord::Schema.define(:version => 20120406152447) do t.boolean "featured", :default => false t.string "username" t.text "snapshot" + t.integer "comments_count", :default => 0, :null => false end add_index "asciicasts", ["created_at"], :name => "index_asciicasts_on_created_at"