mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Resolve association class correctly when assigning ids on a through association
This commit is contained in:
parent
0951306ca5
commit
847e9dd5c1
3 changed files with 12 additions and 1 deletions
|
@ -75,7 +75,7 @@ module ActiveRecord
|
||||||
r.send(reflection.association_primary_key)
|
r.send(reflection.association_primary_key)
|
||||||
end.values_at(*ids).compact
|
end.values_at(*ids).compact
|
||||||
if records.size != ids.size
|
if records.size != ids.size
|
||||||
scope.raise_record_not_found_exception!(ids, records.size, ids.size, reflection.association_primary_key)
|
klass.all.raise_record_not_found_exception!(ids, records.size, ids.size, reflection.association_primary_key)
|
||||||
else
|
else
|
||||||
replace(records)
|
replace(records)
|
||||||
end
|
end
|
||||||
|
|
|
@ -850,6 +850,10 @@ module ActiveRecord
|
||||||
actual_source_reflection.options[:primary_key] || primary_key(klass || self.klass)
|
actual_source_reflection.options[:primary_key] || primary_key(klass || self.klass)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def association_primary_key_type
|
||||||
|
klass.type_for_attribute(association_primary_key)
|
||||||
|
end
|
||||||
|
|
||||||
# Gets an array of possible <tt>:through</tt> source reflection names in both singular and plural form.
|
# Gets an array of possible <tt>:through</tt> source reflection names in both singular and plural form.
|
||||||
#
|
#
|
||||||
# class Post < ActiveRecord::Base
|
# class Post < ActiveRecord::Base
|
||||||
|
|
|
@ -904,6 +904,13 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase
|
||||||
assert_match(/Couldn't find all Clients with 'name'/, e.message)
|
assert_match(/Couldn't find all Clients with 'name'/, e.message)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_collection_singular_ids_through_setter_raises_exception_when_invalid_ids_set
|
||||||
|
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
|
||||||
|
end
|
||||||
|
|
||||||
def test_build_a_model_from_hm_through_association_with_where_clause
|
def test_build_a_model_from_hm_through_association_with_where_clause
|
||||||
assert_nothing_raised { books(:awdr).subscribers.where(nick: "marklazz").build }
|
assert_nothing_raised { books(:awdr).subscribers.where(nick: "marklazz").build }
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue