提交 702e36e2 编写于 作者: G Gaurish Sharma

Return Not found Ids in ActiveRecord::NotFound

This builds on top of 15e2da65.
now it also returns exact Ids which were not found which will be debugging simple.
上级 904f1a87
......@@ -59,7 +59,9 @@ def ids_writer(ids)
r.send(reflection.association_primary_key)
end.values_at(*ids).compact
if records.size != ids.size
klass.all.raise_record_not_found_exception!(ids, records.size, ids.size, reflection.association_primary_key)
found_ids = records.map { |record| record.send(reflection.association_primary_key) }
not_found_ids = ids - found_ids
klass.all.raise_record_not_found_exception!(ids, records.size, ids.size, reflection.association_primary_key, not_found_ids)
else
replace(records)
end
......
......@@ -330,7 +330,7 @@ def exists?(conditions = :none)
# of results obtained should be provided in the +result_size+ argument and
# the expected number of results should be provided in the +expected_size+
# argument.
def raise_record_not_found_exception!(ids = nil, result_size = nil, expected_size = nil, key = primary_key) # :nodoc:
def raise_record_not_found_exception!(ids = nil, result_size = nil, expected_size = nil, key = primary_key, not_found_ids = nil) # :nodoc:
conditions = arel.where_sql(@klass)
conditions = " [#{conditions}]" if conditions
name = @klass.name
......@@ -344,8 +344,8 @@ def raise_record_not_found_exception!(ids = nil, result_size = nil, expected_siz
raise RecordNotFound.new(error, name, key, ids)
else
error = "Couldn't find all #{name.pluralize} with '#{key}': ".dup
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})."
error << " Couldn't find #{name.pluralize(not_found_ids.size)} with #{key.to_s.pluralize(not_found_ids.size)} #{not_found_ids.join(', ')}." if not_found_ids
raise RecordNotFound.new(error, name, primary_key, ids)
end
end
......
......@@ -891,7 +891,8 @@ def test_collection_singular_ids_setter_raises_exception_when_invalid_ids_set
company = companies(:rails_core)
ids = [Developer.first.id, -9999]
e = assert_raises(ActiveRecord::RecordNotFound) { company.developer_ids = ids }
assert_match(/Couldn't find all Developers with 'id'/, e.message)
msg = "Couldn't find all Developers with 'id': (1, -9999) (found 1 results, but was looking for 2). Couldn't find Developer with id -9999."
assert_equal(msg, e.message)
end
def test_collection_singular_ids_setter_raises_exception_when_invalid_ids_set_with_changed_primary_key
......@@ -905,7 +906,8 @@ def test_collection_singular_ids_through_setter_raises_exception_when_invalid_id
author = authors(:david)
ids = [categories(:general).name, "Unknown"]
e = assert_raises(ActiveRecord::RecordNotFound) { author.essay_category_ids = ids }
assert_equal "Couldn't find all Categories with 'name': (General, Unknown) (found 1 results, but was looking for 2)", e.message
msg = "Couldn't find all Categories with 'name': (General, Unknown) (found 1 results, but was looking for 2). Couldn't find Category with name Unknown."
assert_equal msg, e.message
end
def test_build_a_model_from_hm_through_association_with_where_clause
......
......@@ -1153,7 +1153,7 @@ def test_find_some_message_with_custom_primary_key
e = assert_raises(ActiveRecord::RecordNotFound) do
model.find "Hello", "World!"
end
assert_equal "Couldn't find all MercedesCars with 'name': (Hello, World!) (found 0 results, but was looking for 2)", e.message
assert_equal "Couldn't find all MercedesCars with 'name': (Hello, World!) (found 0 results, but was looking for 2).", e.message
end
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册