sql commands converted to active record