I have an expenses table. It has a column invoice_id.
I would like 2 scopes – one for billed expenses and one for not billed.
I thought I could just test to see if invoice_id was nil.
scope :notbilled, where(:invoice_id == nil)
scope :billed, where(:invoice_id != nil)
But, that doesn’t work.
Any ideas?
Thanks!
For the first one, you need to use hash syntax:
The second requires you to use raw sql:
You can also jump down into arel for the second one to avoid writing raw sql, but that’s an advanced topic and would end up less readable than the raw sql.
Using direct equality won’t work, because those expressions are evaluated immediately, and are turned into their boolean equivalents because no variables are involved, so they would be interpreted as