D
D
Demigodd2019-02-05 14:09:54
Ruby on Rails
Demigodd, 2019-02-05 14:09:54

How to insert a condition for scope, during where?

For example, there is this code

scope :age, -> { age >= 99 }

Person.where(name: 'Alex').age

How to put a condition here?
If for example True then Person.where(name: 'Alex').age , if False then without scope Person.where(name: 'Alex')
Person.where(name: 'Alex').filter

def filter

  if expression
    age #scope
  end

end

It is clear that the code above will not work, but I need something like this.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
oh_shi, 2019-02-05
@Demigodd

I already answered almost the same question How to return all Models if id is nil in Rails?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question