0

Here is my code, error comes back with an error at or near WHERE

first_of_month = Date.current.beginning_of_month
last_of_month = Date.current.end_of_month
today = Date.current
query = quotes.where('close_date BETWEEN ? AND ? AND status_id = 8 WHERE quote_exp < ? ', first_of_month, last_of_month, today)

query.sum(:total)
1
  • Why do you have a WHERE clause within your where() function? Commented Jun 17, 2016 at 20:52

3 Answers 3

1

Based on your explanatory comments, you could write the query like this:

query = query.where(<<-EOQ, first_of_month, last_of_month, today)
  close_date BETWEEN ? AND ?
  AND (quote_exp >= ? OR status_id = 8)
EOQ

That will give you: (1) everything from the month (2) that is either not expired or has status 8. Remember that A->B is also ~AvB.

Sign up to request clarification or add additional context in comments.

2 Comments

This I feel is what I am looking for. I will plug it in and let you know how it goes.
Worked like a charm. Thanks Paul!
0

Your 4th line should not contain the WHERE keyword. You should replace it with AND if your intent is to add the other criterion to the rest, like so:

query = quotes.where('close_date BETWEEN ? AND ? AND status_id = 8 AND quote_exp < ? ', first_of_month, last_of_month, today)

1 Comment

your second AND would set it to only include quotes that close within that period and have a status_id of 8 AND and expiration date before today... right? I'm trying to select all between the period and if there are expired ones before today, then include them only if they have a status_id = 8
0

You could write it better in this way.

query = quotes.where(close_date: first_of_month..last_of_month, status_id: 8).where('quote_exp < ?', today).sum(:total)

1 Comment

I'm trying to select all quotes with a close date between the period and if there are expired ones before today, then include them only if they have a status_id = 8. Would this do that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.