1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

When calling association.find RecordNotFound is now raised with the same argument as when we do it in Record.find (primary_key, id and model).

This commit is contained in:
Michel Pigassou 2016-07-30 15:19:30 +02:00
parent 6107a40c0e
commit cafdbeba90
4 changed files with 33 additions and 4 deletions

View file

@ -1,3 +1,8 @@
* RecordNotFound raised by association.find exposes `id`, `primary_key` and
`model` methods to be consistent with RecordNotFound raised by Record.find.
*Michel Pigassou*
* Hashes can once again be passed to setters of `composed_of`, if all of the
mapping methods are methods implemented on `Hash`.

View file

@ -348,16 +348,18 @@ module ActiveRecord
def raise_record_not_found_exception!(ids, result_size, expected_size) #:nodoc:
conditions = arel.where_sql(@klass.arel_engine)
conditions = " [#{conditions}]" if conditions
name = @klass.name
if Array(ids).size == 1
error = "Couldn't find #{@klass.name} with '#{primary_key}'=#{ids}#{conditions}"
error = "Couldn't find #{name} with '#{primary_key}'=#{ids}#{conditions}"
raise RecordNotFound.new(error, name, primary_key, ids)
else
error = "Couldn't find all #{@klass.name.pluralize} with '#{primary_key}': "
error = "Couldn't find all #{name.pluralize} with '#{primary_key}': "
error << "(#{ids.join(", ")})#{conditions} (found #{result_size} results, but was looking for #{expected_size})"
end
raise RecordNotFound, error
end
end
private

View file

@ -618,6 +618,18 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
assert_raise(ActiveRecord::RecordNotFound) { firm.clients.find(2, 99) }
end
def test_find_one_message_on_primary_key
firm = Firm.all.merge!(order: "id").first
e = assert_raises(ActiveRecord::RecordNotFound) do
firm.clients.find(0)
end
assert_equal 0, e.id
assert_equal "id", e.primary_key
assert_equal "Client", e.model
assert_match (/\ACouldn't find Client with 'id'=0/), e.message
end
def test_find_ids_and_inverse_of
force_signal37_to_load_all_clients_of_firm

View file

@ -1119,6 +1119,16 @@ class FinderTest < ActiveRecord::TestCase
assert_equal [0, 1, 1], posts.map(&:author_id).sort
end
def test_find_one_message_on_primary_key
e = assert_raises(ActiveRecord::RecordNotFound) do
Car.find(0)
end
assert_equal 0, e.id
assert_equal "id", e.primary_key
assert_equal "Car", e.model
assert_equal "Couldn't find Car with 'id'=0", e.message
end
def test_find_one_message_with_custom_primary_key
table_with_custom_primary_key do |model|
model.primary_key = :name