Return an Empty Active Record Collection
You can use .none
in a scope to short circuit the query in the event you don't have all the data.
Imagine this query but the project_type
on a Project
is nil
class User
scope :active -> { where(archived: nil }
scope :by_project, -> (project) do
return none unless project.type.present?
where(project_guid: project.guid, role: project.type)
end
end
Just return none
.
The cool thing about this is it's chainable. So you can still do something like:
project = Project.new(project_type: nil)
User.by_project(project).active
Tweet