refactor get_by_unique to use a single query