I have defined two scope-like methods on a model:
def self.foo var
where(foo: var)
end
def self.bar var
where(bar: var)
end
I want to be able to pass nil to one of these methods and have it effectively be ignored. So:
var1 = 10
var2 = nil
# ...
Model.foo(var1).bar(var2)
I've tried various things such as:
def self.bar var
return self if var.nil?
where(bar: var)
end
but in the above instance, self
doesn't return what this method has been passed from the previous method in the chain, it returns Model
, therefore I lose all the legwork done in foo
.
How can I achieve what I'm trying?