osem-fcy/db/migrate/20190720062321_add_comments_count_to_tracks.rb
2019-07-23 01:41:34 +05:30

10 lines
342 B
Ruby

class AddCommentsCountToTracks < ActiveRecord::Migration[5.1]
def change
add_column :tracks, :comments_count, :integer, default: 0, null: false
Track.find_each do |track|
comments_count = track.comment_threads.count
track.update_attributes(:comments_count, comments_count) unless comments_count.zero?
end
end
end