aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/relations_test.rb
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2012-03-30 12:44:45 +0100
committerJon Leighton <j@jonathanleighton.com>2012-03-30 12:52:29 +0100
commit13b3c77e393b8fb02588f39e6bfa10c832e251ff (patch)
tree01bbb86cd94d6f18060d70c8a2d7ec8bd85ed0a4 /activerecord/test/cases/relations_test.rb
parent3a8c54396ea3965eb7601501d7bb9618ff305728 (diff)
downloadrails-13b3c77e393b8fb02588f39e6bfa10c832e251ff.tar.gz
rails-13b3c77e393b8fb02588f39e6bfa10c832e251ff.tar.bz2
rails-13b3c77e393b8fb02588f39e6bfa10c832e251ff.zip
Add Relation#find_by and Relation#find_by!
Diffstat (limited to 'activerecord/test/cases/relations_test.rb')
-rw-r--r--activerecord/test/cases/relations_test.rb34
1 files changed, 34 insertions, 0 deletions
diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb
index 3edc237c44..25eb7c1672 100644
--- a/activerecord/test/cases/relations_test.rb
+++ b/activerecord/test/cases/relations_test.rb
@@ -1256,4 +1256,38 @@ class RelationTest < ActiveRecord::TestCase
assert topics.loaded?
end
+
+ test "find_by with hash conditions returns the first matching record" do
+ assert_equal posts(:eager_other), Post.order(:id).find_by(author_id: 2)
+ end
+
+ test "find_by with non-hash conditions returns the first matching record" do
+ assert_equal posts(:eager_other), Post.order(:id).find_by("author_id = 2")
+ end
+
+ test "find_by with multi-arg conditions returns the first matching record" do
+ assert_equal posts(:eager_other), Post.order(:id).find_by('author_id = ?', 2)
+ end
+
+ test "find_by returns nil if the record is missing" do
+ assert_equal nil, Post.scoped.find_by("1 = 0")
+ end
+
+ test "find_by! with hash conditions returns the first matching record" do
+ assert_equal posts(:eager_other), Post.order(:id).find_by!(author_id: 2)
+ end
+
+ test "find_by! with non-hash conditions returns the first matching record" do
+ assert_equal posts(:eager_other), Post.order(:id).find_by!("author_id = 2")
+ end
+
+ test "find_by! with multi-arg conditions returns the first matching record" do
+ assert_equal posts(:eager_other), Post.order(:id).find_by!('author_id = ?', 2)
+ end
+
+ test "find_by! raises RecordNotFound if the record is missing" do
+ assert_raises(ActiveRecord::RecordNotFound) do
+ Post.scoped.find_by!("1 = 0")
+ end
+ end
end