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:
parent
6107a40c0e
commit
cafdbeba90
4 changed files with 33 additions and 4 deletions
|
@ -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
|
* Hashes can once again be passed to setters of `composed_of`, if all of the
|
||||||
mapping methods are methods implemented on `Hash`.
|
mapping methods are methods implemented on `Hash`.
|
||||||
|
|
||||||
|
|
|
@ -348,15 +348,17 @@ module ActiveRecord
|
||||||
def raise_record_not_found_exception!(ids, result_size, expected_size) #:nodoc:
|
def raise_record_not_found_exception!(ids, result_size, expected_size) #:nodoc:
|
||||||
conditions = arel.where_sql(@klass.arel_engine)
|
conditions = arel.where_sql(@klass.arel_engine)
|
||||||
conditions = " [#{conditions}]" if conditions
|
conditions = " [#{conditions}]" if conditions
|
||||||
|
name = @klass.name
|
||||||
|
|
||||||
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 #{name} with '#{primary_key}'=#{ids}#{conditions}"
|
||||||
|
raise RecordNotFound.new(error, name, primary_key, ids)
|
||||||
else
|
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})"
|
error << "(#{ids.join(", ")})#{conditions} (found #{result_size} results, but was looking for #{expected_size})"
|
||||||
end
|
|
||||||
|
|
||||||
raise RecordNotFound, error
|
raise RecordNotFound, error
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
|
@ -618,6 +618,18 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
|
||||||
assert_raise(ActiveRecord::RecordNotFound) { firm.clients.find(2, 99) }
|
assert_raise(ActiveRecord::RecordNotFound) { firm.clients.find(2, 99) }
|
||||||
end
|
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
|
def test_find_ids_and_inverse_of
|
||||||
force_signal37_to_load_all_clients_of_firm
|
force_signal37_to_load_all_clients_of_firm
|
||||||
|
|
||||||
|
|
|
@ -1119,6 +1119,16 @@ class FinderTest < ActiveRecord::TestCase
|
||||||
assert_equal [0, 1, 1], posts.map(&:author_id).sort
|
assert_equal [0, 1, 1], posts.map(&:author_id).sort
|
||||||
end
|
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
|
def test_find_one_message_with_custom_primary_key
|
||||||
table_with_custom_primary_key do |model|
|
table_with_custom_primary_key do |model|
|
||||||
model.primary_key = :name
|
model.primary_key = :name
|
||||||
|
|
Loading…
Reference in a new issue