mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Ensure custom PK types are casted in through reflection queries
Make sure primary keys with non-integer types can be correctly type casted in through reflections.
This commit is contained in:
parent
f66bfdb284
commit
5ec2f35f27
6 changed files with 26 additions and 1 deletions
|
@ -1,3 +1,7 @@
|
|||
* Ensure custom PK types are casted in through reflection queries.
|
||||
|
||||
*Gannon McGibbon*
|
||||
|
||||
* Preserve user supplied joins order as much as possible.
|
||||
|
||||
Fixes #36761, #34328, #24281, #12953.
|
||||
|
|
|
@ -764,7 +764,16 @@ module ActiveRecord
|
|||
end
|
||||
|
||||
def klass
|
||||
@klass ||= delegate_reflection.compute_class(class_name)
|
||||
@klass ||= delegate_reflection.compute_class(class_name).tap do |klass|
|
||||
if !parent_reflection.is_a?(HasAndBelongsToManyReflection) &&
|
||||
!klass.reflections.include?(options[:through].to_s) &&
|
||||
active_record.type_for_attribute(active_record.primary_key).type != :integer
|
||||
raise NotImplementedError, <<~MSG.squish
|
||||
In order to correctly type cast #{active_record}.#{active_record.primary_key},
|
||||
#{klass} needs to define a :#{options[:through]} association.
|
||||
MSG
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Returns the source of the through reflection. It checks both a singularized
|
||||
|
|
|
@ -256,6 +256,14 @@ class ReflectionTest < ActiveRecord::TestCase
|
|||
assert_kind_of ThroughReflection, Subscriber.reflect_on_association(:books)
|
||||
end
|
||||
|
||||
def test_uncastable_has_many_through_reflection
|
||||
error = assert_raises(NotImplementedError) { Subscriber.new.published_books }
|
||||
assert_equal <<~MSG.squish, error.message
|
||||
In order to correctly type cast Subscriber.nick,
|
||||
PublishedBook needs to define a :subscriptions association.
|
||||
MSG
|
||||
end
|
||||
|
||||
def test_chain
|
||||
expected = [
|
||||
Organization.reflect_on_association(:author_essay_categories),
|
||||
|
|
|
@ -2,4 +2,6 @@
|
|||
|
||||
class Dashboard < ActiveRecord::Base
|
||||
self.primary_key = :dashboard_id
|
||||
|
||||
has_one :speedometer
|
||||
end
|
||||
|
|
|
@ -4,6 +4,7 @@ class Subscriber < ActiveRecord::Base
|
|||
self.primary_key = "nick"
|
||||
has_many :subscriptions
|
||||
has_many :books, through: :subscriptions
|
||||
has_many :published_books, through: :subscriptions
|
||||
end
|
||||
|
||||
class SpecialSubscriber < Subscriber
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
class Subscription < ActiveRecord::Base
|
||||
belongs_to :subscriber, counter_cache: :books_count
|
||||
belongs_to :published_book
|
||||
belongs_to :book
|
||||
|
||||
validates_presence_of :subscriber_id, :book_id
|
||||
|
|
Loading…
Reference in a new issue