Answer the question
In order to leave comments, you need to log in
How to return all Models if id is nil in Rails?
How to return all Models if id is nil in Rails ?
arr_id = [1, 2, 3]
Model.where(id: arr_id)
Answer the question
In order to leave comments, you need to log in
def self.filter_id(arr_id)
if arr_id.present?
where(id: arr_id)
else
all
end
end
scope :filter_id, -> arr_id { where(id: arr_id) if arr_id.present? }
if arr_id.nil? || arr_id.empty?
Model.all
else
Model.where id: arr_id
end
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question