class CreateIpHistories < ActiveRecord::Migration
  def change
    create_table :ip_histories do |t|
      t.string :remote_ip
      t.string :country
      t.string :city
      t.decimal :latitude, precision: 10, scale: 6
      t.decimal :longitude, precision: 10, scale: 6
      t.integer :element_id
      t.string :element_type
      t.references :annonce_account
      t.timestamps null: false
    end
    
    AnnonceMessage.all.each do |annonce_message|
      if annonce_message.remote_ip? and annonce_message.expediteur
        IpHistory.create(:remote_ip => annonce_message.remote_ip, :element => annonce_message, :annonce_account => annonce_message.expediteur)
      
      end
    end
    
    AnnonceAccount.all.each do |annonce_account|
      if annonce_account.remote_ip? 
        IpHistory.create(:remote_ip => annonce_account.remote_ip, :element => annonce_account, :annonce_account => annonce_account)
      
      end
      if annonce_account.last_sign_in_ip? 
        IpHistory.create(:remote_ip => annonce_account.last_sign_in_ip, :element => annonce_account, :annonce_account => annonce_account)
      
      end
      if annonce_account.current_sign_in_ip? 
        IpHistory.create(:remote_ip => annonce_account.current_sign_in_ip, :element => annonce_account, :annonce_account => annonce_account)
      
      end
    end
    
    
    
  end
end
