Question: I want to produce the following sql using active record.

```
WHERE (column_name1, column_name1) IN (SELECT ....)

```

I don't know how to do this is active record.

I've tried these so far

```
where('column_name1, column_name2' => {})
where([:column_name1, :column_name2] => {})

```

This is the full query I'd like to create

```
SELECT a, Count(1) 
            FROM   table 
            WHERE  ( a, b ) IN (SELECT a, 
                                    Max(b) 
                                    FROM   table 
                                    GROUP  BY a) 
            GROUP  BY a 
            HAVING Count(1) > 1)

```

I've already written a scope for the subquery

Thanks in advance.

Answer: 
It is because after you set validInput to false in the first run of the loop with an invalid input, there is no code to set it to true in other attempts, hece going into an infinite loop, i.e. the while condition always fails.

What you need to do is set valdInput to true when the answer is within the valid expected values. For instance, modifying your original code:

```
bool validInput = true;
    do
{
            cout << "Choose your attacking animal:" << endl;
    cout << "1 = Bunny, 2 = Chicken, 3 = Kitty, 4 = Puppy, 5 = Wolverine" <<          endl;
    cin >> attacker;

    if (attacker < 1 || attacker > 5 || cin.fail())
    {
        cout << "Number must be 1 through 5" << endl;

        cin.clear();
        cin.ignore();
        validInput = false;
    } else {
        validInput = true;
    }
} while (!validInput);

cout << "Choose how many " << attacker<< " are attacking"<<endl;
cin >> attNum;

```

Let me know if this helps.
As LarsEggert already stated you never set back back the valid-flag to true if the input was in fact valid.

I would solve it in the following way:

```
bool validInput;
do {
  // Assumption: Input will be valid (Loop will not repeat in this case)
  validInput = true;
  cout << "Choose your attacking animal:" << endl;
  cout << "1 = Bunny, 2 = Chicken, 3 = Kitty, 4 = Puppy, 5 = Wolverine" << endl;
  cin >> attacker;

  // Check if assumption was wrong (Loop will run again in this case)
  if (attacker < 1 || attacker > 5 || cin.fail())
  {
    cout << "Number must be 1 through 5" << endl;

    cin.clear();
    cin.ignore();
    validInput = false;
  }
} while (!validInput);

cout << "Choose how many " << attacker<< " are attacking"<<endl;
cin >> attNum;

```

In compare to Jorge Torres's answer you won't have to write the else-case.

I typically follow this form for loops:

```
bool repeat;
do {
  repeat = false

  // do something
  // if loop has to be repeated, just set repeat = true somewhere here
} while(repeat);

```

This way you don't have a negation in your while and you also just have to focus on the cases where the repeat-flag has to be set.