提交 5ec2f35f 编写于 作者: G Gannon McGibbon

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.
上级 f66bfdb2
* 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 @@ def through_reflection?
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 @@ def test_has_many_through_reflection
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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册