Today I Learned

hashrocket A Hashrocket project

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
See More #rails TILs
Looking for help? Hashrocket has been an industry leader in Ruby on Rails since 2008. Rails is a core skill for each developer at Hashrocket, and we'd love to take a look at your project. Contact us and find out how we can help you.