Chris Hough
Chris Hough

Reputation: 3558

Rails 4 scope NOTNULL refactor

I had to create a scope to create active jobs, but this feels a little odd and honestly it's tightly coupled to PosgresSQL:

  scope :active, -> { where('reviewed_at NOTNULL and paid_at NOTNULL and end_at >= ?', Date.today) }

Would you write this differently?

Upvotes: 1

Views: 36

Answers (2)

Hieu Pham
Hieu Pham

Reputation: 6692

A shorter & more beautiful version will be like this:

scope :active, -> { where.not(reviewed_at: nil, paid_at: nil).where('end_at >= ?', Date.today) }

Upvotes: 2

fengd
fengd

Reputation: 7579

you can use where.not in rails 4

scope :active, -> { where.not(reviewed_at: nil).where.not(paid_at: nil).where('end_at >= ?', Date.today) }

Upvotes: 0

Related Questions