From ea35ccfe2014aff208601b98442ff03fa76269a9 Mon Sep 17 00:00:00 2001 From: Emilio Tagua Date: Wed, 22 Sep 2010 11:21:28 -0300 Subject: [PATCH] Perf: refactor method. Signed-off-by: Santiago Pastorino --- .../has_and_belongs_to_many_association.rb | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb index 605bc9c66f..eb65234dfb 100644 --- a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb +++ b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb @@ -49,23 +49,20 @@ def insert_record(record, force = true, validate = true) timestamps = record_timestamp_columns(record) timezone = record.send(:current_time_from_proper_timezone) if timestamps.any? - attributes = columns.inject({}) do |attrs, column| + attributes = Hash[columns.map do |column| name = column.name - case name.to_s + value = case name.to_s when @reflection.primary_key_name.to_s - attrs[relation[name]] = @owner.id + @owner.id when @reflection.association_foreign_key.to_s - attrs[relation[name]] = record.id + record.id when *timestamps - attrs[relation[name]] = timezone + timezone else - if record.has_attribute?(name) - value = @owner.send(:quote_value, record[name], column) - attrs[relation[name]] = value unless value.nil? - end + @owner.send(:quote_value, record[name], column) if record.has_attribute?(name) end - attrs - end + [relation[name], value] unless value.nil? + end] relation.insert(attributes) end -- GitLab