osem-fcy/db/migrate/20161229080315_add_comments_count_to_events.rb
2017-07-09 01:57:44 +05:30

10 lines
336 B
Ruby

class AddCommentsCountToEvents < ActiveRecord::Migration
def change
add_column :events, :comments_count, :integer, default: 0, null: false
Event.find_each do |event|
comments_count = event.comment_threads.count
event.update_attribute(:comments_count, comments_count) unless comments_count.zero?
end
end
end