aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2013-10-08 10:49:46 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2013-10-08 10:49:53 -0700
commit40898c8c19fa04442fc5f8fb5daf3a8bdb9a1e03 (patch)
tree975f29743ffee4078ae687c2838d9c13929aecb5 /activerecord/lib/active_record/associations
parent4cf2f9d57046603c80f8f47a7d17cc6447acd3e4 (diff)
downloadrails-40898c8c19fa04442fc5f8fb5daf3a8bdb9a1e03.tar.gz
rails-40898c8c19fa04442fc5f8fb5daf3a8bdb9a1e03.tar.bz2
rails-40898c8c19fa04442fc5f8fb5daf3a8bdb9a1e03.zip
typecast records returned from the db rather than to_sing everything
Diffstat (limited to 'activerecord/lib/active_record/associations')
-rw-r--r--activerecord/lib/active_record/associations/join_dependency.rb29
1 files changed, 17 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