cirandas.net

ref: master

db/migrate/20140312144156_define_initial_value_for_profiles_friends_count.rb


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class DefineInitialValueForProfilesFriendsCount < ActiveRecord::Migration
  def self.up
    friends_counts = execute("SELECT profiles.id, count(profiles.id) FROM profiles INNER JOIN friendships ON ( profiles.id = friendships.friend_id AND profiles.type = E'Person') GROUP BY profiles.id;")
    friends_counts.each do |count|
      execute("UPDATE profiles SET friends_count=#{count['count'].to_i} WHERE profiles.id=#{count['id']};")
    end
  end

  def self.down
      execute("UPDATE profiles SET friends_count=0;")
  end
end