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
@ -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.
|
* Preserve user supplied joins order as much as possible.
|
||||||
|
|
||||||
Fixes #36761, #34328, #24281, #12953.
|
Fixes #36761, #34328, #24281, #12953.
|
||||||
|
@ -764,7 +764,16 @@ def through_reflection?
|
|||||||
end
|
end
|
||||||
|
|
||||||
def klass
|
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
|
end
|
||||||
|
|
||||||
# Returns the source of the through reflection. It checks both a singularized
|
# 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)
|
assert_kind_of ThroughReflection, Subscriber.reflect_on_association(:books)
|
||||||
end
|
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
|
def test_chain
|
||||||
expected = [
|
expected = [
|
||||||
Organization.reflect_on_association(:author_essay_categories),
|
Organization.reflect_on_association(:author_essay_categories),
|
||||||
|
@ -2,4 +2,6 @@
|
|||||||
|
|
||||||
class Dashboard < ActiveRecord::Base
|
class Dashboard < ActiveRecord::Base
|
||||||
self.primary_key = :dashboard_id
|
self.primary_key = :dashboard_id
|
||||||
|
|
||||||
|
has_one :speedometer
|
||||||
end
|
end
|
||||||
|
@ -4,6 +4,7 @@ class Subscriber < ActiveRecord::Base
|
|||||||
self.primary_key = "nick"
|
self.primary_key = "nick"
|
||||||
has_many :subscriptions
|
has_many :subscriptions
|
||||||
has_many :books, through: :subscriptions
|
has_many :books, through: :subscriptions
|
||||||
|
has_many :published_books, through: :subscriptions
|
||||||
end
|
end
|
||||||
|
|
||||||
class SpecialSubscriber < Subscriber
|
class SpecialSubscriber < Subscriber
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
class Subscription < ActiveRecord::Base
|
class Subscription < ActiveRecord::Base
|
||||||
belongs_to :subscriber, counter_cache: :books_count
|
belongs_to :subscriber, counter_cache: :books_count
|
||||||
|
belongs_to :published_book
|
||||||
belongs_to :book
|
belongs_to :book
|
||||||
|
|
||||||
validates_presence_of :subscriber_id, :book_id
|
validates_presence_of :subscriber_id, :book_id
|
||||||
|
Loading…
Reference in New Issue
Block a user