aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r--activerecord/lib/active_record/associations/join_dependency.rb29
-rw-r--r--activerecord/lib/active_record/result.rb4
2 files changed, 21 insertions, 12 deletions
diff --git a/activerecord/lib/active_record/associations/join_dependency.rb b/activerecord/lib/active_record/associations/join_dependency.rb
index 6d3b1b6b3b..7ae4d1763d 100644
--- a/activerecord/lib/active_record/associations/join_dependency.rb
+++ b/activerecord/lib/active_record/associations/join_dependency.rb
@@ -69,14 +69,16 @@ module ActiveRecord
}.flatten
end
- def instantiate(rows)
+ def instantiate(result_set)
primary_key = join_base.aliased_primary_key
parents = {}
- records = rows.map { |model|
- primary_id = model[primary_key]
- parent = parents[primary_id] ||= join_base.instantiate(model)
- construct(parent, @associations, join_associations, model)
+ type_caster = result_set.column_type primary_key
+
+ records = result_set.map { |row_hash|
+ primary_id = type_caster.type_cast row_hash[primary_key]
+ parent = parents[primary_id] ||= join_base.instantiate(row_hash)
+ construct(parent, @associations, join_associations, row_hash, result_set)
parent
}.uniq
@@ -181,14 +183,14 @@ module ActiveRecord
JoinAssociation.new(reflection, self, parent, join_type)
end
- def construct(parent, associations, join_parts, row)
+ def construct(parent, associations, join_parts, row, rs)
associations.sort_by { |k,_| k.to_s }.each do |association_name, assoc|
- association = construct_scalar(parent, association_name, join_parts, row)
- construct(association, assoc, join_parts, row) if association
+ association = construct_scalar(parent, association_name, join_parts, row, rs)
+ construct(association, assoc, join_parts, row, rs) if association
end
end
- def construct_scalar(parent, associations, join_parts, row)
+ def construct_scalar(parent, associations, join_parts, row, rs)
name = associations.to_s
join_part = join_parts.detect { |j|
@@ -199,11 +201,14 @@ module ActiveRecord
raise(ConfigurationError, "No such association") unless join_part
join_parts.delete(join_part)
- construct_association(parent, join_part, row)
+ construct_association(parent, join_part, row, rs)
end
- def construct_association(record, join_part, row)
- return if record.id.to_s != join_part.parent.record_id(row).to_s
+ def construct_association(record, join_part, row, rs)
+ caster = rs.column_type(join_part.parent.aliased_primary_key)
+ row_id = caster.type_cast row[join_part.parent.aliased_primary_key]
+
+ return if record.id != row_id
macro = join_part.reflection.macro
if macro == :has_one
diff --git a/activerecord/lib/active_record/result.rb b/activerecord/lib/active_record/result.rb
index d0f1cb5b75..1dc3bf3f12 100644
--- a/activerecord/lib/active_record/result.rb
+++ b/activerecord/lib/active_record/result.rb
@@ -46,6 +46,10 @@ module ActiveRecord
IDENTITY_TYPE
end
+ def column_type(name)
+ @column_types[name] || identity_type
+ end
+
def each
if block_given?
hash_rows.each { |row| yield row }