diff options
author | Sean Griffin <sean@thoughtbot.com> | 2015-02-04 08:44:48 -0700 |
---|---|---|
committer | Sean Griffin <sean@thoughtbot.com> | 2015-02-04 08:56:46 -0700 |
commit | cd0ed12d1a9edc73be953d700748b5827df890c7 (patch) | |
tree | 86d2e0f466213a95f360164e421665bde5a6bdcd /activerecord/lib/active_record/relation/predicate_builder | |
parent | 1405c7a2cb3539880ebd82c287040b55d289a427 (diff) | |
download | rails-cd0ed12d1a9edc73be953d700748b5827df890c7.tar.gz rails-cd0ed12d1a9edc73be953d700748b5827df890c7.tar.bz2 rails-cd0ed12d1a9edc73be953d700748b5827df890c7.zip |
Respect custom primary keys for associations in `Relation#where`
While we query the proper columns, we go through normal handling for
converting the value to a primitive which assumes it should use the
table's primary key. If the association specifies a different value (and
we know that we're working with an association), we should use the
custom primary key instead.
Fixes #18813.
Diffstat (limited to 'activerecord/lib/active_record/relation/predicate_builder')
-rw-r--r-- | activerecord/lib/active_record/relation/predicate_builder/association_query_handler.rb | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/relation/predicate_builder/association_query_handler.rb b/activerecord/lib/active_record/relation/predicate_builder/association_query_handler.rb index aabcf20c1d..159889d3b8 100644 --- a/activerecord/lib/active_record/relation/predicate_builder/association_query_handler.rb +++ b/activerecord/lib/active_record/relation/predicate_builder/association_query_handler.rb @@ -31,7 +31,14 @@ module ActiveRecord end def ids - value + case value + when Relation + value.select(primary_key) + when Array + value.map { |v| convert_to_id(v) } + else + convert_to_id(value) + end end def base_class @@ -42,6 +49,10 @@ module ActiveRecord private + def primary_key + associated_table.association_primary_key(base_class) + end + def polymorphic_base_class_from_value case value when Relation @@ -53,6 +64,15 @@ module ActiveRecord value.class.base_class end end + + def convert_to_id(value) + case value + when Base + value._read_attribute(primary_key) + else + value + end + end end end end |