Skip to content

Instantly share code, notes, and snippets.

@tbaba
Last active December 11, 2015 04:18
Show Gist options
  • Save tbaba/4543857 to your computer and use it in GitHub Desktop.
Save tbaba/4543857 to your computer and use it in GitHub Desktop.
ActiveRecord::QueryMethods#all!
Gem::Specification.new do |gem|
gem.name = "all_or_nothing"
gem.version = '0.0.1'
gem.authors = ["Tatsuro Baba"]
gem.email = ["harakirisoul@gmail.com"]
gem.description = %q{TODO: Write a gem description}
gem.summary = %q{TODO: Write a gem summary}
gem.homepage = "https://gist.github.com/4543857"
gem.files = ['all_or_nothing.rb']
gem.require_path = '.'
end
module ActiveRecord
module QueryMethods
def all_or_nothing!
relation = clone
result = relation.all
if result.blank?
raise ActiveRecord::RecordNotFound
else
result
end
end
end
end
@tbaba
Copy link
Author

tbaba commented Jan 16, 2013

This method returns ActiveRecord::RecordNotFound exception if result of this relation is empty.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment