Merge pull request #13785 from kuldeepaggarwal/fix-find_with_multiple_ids

Fix `ActiveRecord::RecordNotFound` error message with custom primary key
This commit is contained in:
Yves Senn 2014-01-24 15:13:16 -08:00
commit 220ee0c74a
2 changed files with 26 additions and 9 deletions

View File

@ -311,9 +311,9 @@ module ActiveRecord
conditions = " [#{conditions}]" if conditions conditions = " [#{conditions}]" if conditions
if Array(ids).size == 1 if Array(ids).size == 1
error = "Couldn't find #{@klass.name} with #{primary_key}=#{ids}#{conditions}" error = "Couldn't find #{@klass.name} with '#{primary_key}'=#{ids}#{conditions}"
else else
error = "Couldn't find all #{@klass.name.pluralize} with IDs " error = "Couldn't find all #{@klass.name.pluralize} with '#{primary_key}': "
error << "(#{ids.join(", ")})#{conditions} (found #{result_size} results, but was looking for #{expected_size})" error << "(#{ids.join(", ")})#{conditions} (found #{result_size} results, but was looking for #{expected_size})"
end end

View File

@ -951,14 +951,23 @@ class FinderTest < ActiveRecord::TestCase
end end
def test_find_one_message_with_custom_primary_key def test_find_one_message_with_custom_primary_key
Toy.primary_key = :name table_with_custom_primary_key do |model|
begin model.primary_key = :name
Toy.find 'Hello World!' e = assert_raises(ActiveRecord::RecordNotFound) do
rescue ActiveRecord::RecordNotFound => e model.find 'Hello World!'
assert_equal 'Couldn\'t find Toy with name=Hello World!', e.message end
assert_equal %Q{Couldn't find MercedesCar with 'name'=Hello World!}, e.message
end
end
def test_find_some_message_with_custom_primary_key
table_with_custom_primary_key do |model|
model.primary_key = :name
e = assert_raises(ActiveRecord::RecordNotFound) do
model.find 'Hello', 'World!'
end
assert_equal %Q{Couldn't find all MercedesCars with 'name': (Hello, World!) (found 0 results, but was looking for 2)}, e.message
end end
ensure
Toy.reset_primary_key
end end
def test_find_without_primary_key def test_find_without_primary_key
@ -979,4 +988,12 @@ class FinderTest < ActiveRecord::TestCase
ActiveRecord::Base.send(:replace_bind_variables, statement, vars) ActiveRecord::Base.send(:replace_bind_variables, statement, vars)
end end
end end
def table_with_custom_primary_key
yield(Class.new(Toy) do
def self.name
'MercedesCar'
end
end)
end
end end