Matrix
Matrix

Reputation: 3369

rails app : same instruction, one use scope, other not, but not same result

I have a scope like this:

scope :not_undo, where(undo: false)

very simple, and I want to do :

activities.not_undo.sum(:foo)

but an error occur:

NoMethodError: undefined method `call' for #<ActiveRecord::Relation []>

In fact, at the begining of my unit test, "activities" are empty. But if I do :

activities.where(undo: false).sum(:foo)

It works, even if it's empty.

So why use scope change the result? How can I do?

thx.

Upvotes: 0

Views: 22

Answers (1)

pragma
pragma

Reputation: 1400

You need to pass lambda as second argument to scope method

scope :not_undo, -> { where(undo: false) }

Upvotes: 2

Related Questions