From 2ea922bcec333b6484a295b4fee1f4fa75c5704d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tarmo=20T=C3=A4nav?= Date: Wed, 10 Jun 2009 16:24:32 +0300 Subject: Don't increment and then decrement the same counter when re-assigning a belongs_to association [#2786 state:committed] Signed-off-by: Santiago Pastorino --- activerecord/lib/active_record/associations/belongs_to_association.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/associations/belongs_to_association.rb b/activerecord/lib/active_record/associations/belongs_to_association.rb index c2a6495db5..4558872a2b 100644 --- a/activerecord/lib/active_record/associations/belongs_to_association.rb +++ b/activerecord/lib/active_record/associations/belongs_to_association.rb @@ -22,7 +22,7 @@ module ActiveRecord else raise_on_type_mismatch(record) - if counter_cache_name && !@owner.new_record? + if counter_cache_name && !@owner.new_record? && record.id != @owner[@reflection.primary_key_name] @reflection.klass.increment_counter(counter_cache_name, record.id) @reflection.klass.decrement_counter(counter_cache_name, @owner[@reflection.primary_key_name]) if @owner[@reflection.primary_key_name] end -- cgit v1.2.3 From d1c53a9ad273f31618adcfbcabb39fd74bb2b91f Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 30 Jul 2010 13:38:09 -0700 Subject: reducing function calls and using faster methods for testing --- activerecord/lib/active_record/relation.rb | 14 +++++++------- activerecord/lib/active_record/relation/query_methods.rb | 12 +++--------- 2 files changed, 10 insertions(+), 16 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index a8cea44c78..7962f52738 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -99,7 +99,7 @@ module ActiveRecord if block_given? to_a.many? { |*block_args| yield(*block_args) } else - @limit_value.present? ? to_a.many? : size > 1 + @limit_value ? to_a.many? : size > 1 end end @@ -316,12 +316,12 @@ module ActiveRecord def scope_for_create @scope_for_create ||= begin - @create_with_value || @where_values.inject({}) do |hash, where| - if where.is_a?(Arel::Predicates::Equality) - hash[where.operand1.name] = where.operand2.respond_to?(:value) ? where.operand2.value : where.operand2 - end - hash - end + @create_with_value || Hash[ + @where_values.grep(Arel::Predicates::Equality).map { |where| + [where.operand1.name, + where.operand2.respond_to?(:value) ? + where.operand2.value : where.operand2] + }] end end diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 716e7275a5..4342dde5c8 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -129,7 +129,7 @@ module ActiveRecord def build_arel arel = table - arel = build_joins(arel, @joins_values) if @joins_values.present? + arel = build_joins(arel, @joins_values) unless @joins_values.empty? @where_values.uniq.each do |where| next if where.blank? @@ -145,7 +145,7 @@ module ActiveRecord arel = arel.having(*@having_values.uniq.select{|h| h.present?}) if @having_values.present? - arel = arel.take(@limit_value) if @limit_value.present? + arel = arel.take(@limit_value) if @limit_value arel = arel.skip(@offset_value) if @offset_value.present? arel = arel.group(*@group_values.uniq.select{|g| g.present?}) if @group_values.present? @@ -155,13 +155,7 @@ module ActiveRecord arel = build_select(arel, @select_values.uniq) arel = arel.from(@from_value) if @from_value.present? - - case @lock_value - when TrueClass - arel = arel.lock - when String - arel = arel.lock(@lock_value) - end if @lock_value.present? + arel = arel.lock(@lock_value) if @lock_value arel end -- cgit v1.2.3 From ecfb252e7a99bac6569f744064ef7f1ee1b31556 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 30 Jul 2010 14:05:26 -0700 Subject: fewer method calls, using faster methods when possible --- activerecord/lib/active_record/relation/query_methods.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 4342dde5c8..b941231a65 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -143,18 +143,18 @@ module ActiveRecord end end - arel = arel.having(*@having_values.uniq.select{|h| h.present?}) if @having_values.present? + arel = arel.having(*@having_values.uniq.select{|h| h.present?}) unless @having_values.empty? arel = arel.take(@limit_value) if @limit_value - arel = arel.skip(@offset_value) if @offset_value.present? + arel = arel.skip(@offset_value) if @offset_value - arel = arel.group(*@group_values.uniq.select{|g| g.present?}) if @group_values.present? + arel = arel.group(*@group_values.uniq.select{|g| g.present?}) unless @group_values.empty? - arel = arel.order(*@order_values.uniq.select{|o| o.present?}) if @order_values.present? + arel = arel.order(*@order_values.uniq.select{|o| o.present?}) unless @order_values.empty? arel = build_select(arel, @select_values.uniq) - arel = arel.from(@from_value) if @from_value.present? + arel = arel.from(@from_value) if @from_value arel = arel.lock(@lock_value) if @lock_value arel -- cgit v1.2.3 From 834e304b223723d63596034ef338d9af6655d35b Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 30 Jul 2010 14:12:01 -0700 Subject: reverting where() functionality, adding a test --- activerecord/lib/active_record/relation/query_methods.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index b941231a65..a92d180442 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -47,8 +47,8 @@ module ActiveRecord clone.tap {|r| r.joins_values += args if args.present? } end - def where(opts, other = nil) - value = build_where(opts, other) + def where(opts, *rest) + value = build_where(opts, rest) value ? clone.tap {|r| r.where_values += Array.wrap(value) } : clone end @@ -160,10 +160,10 @@ module ActiveRecord arel end - def build_where(opts, other = nil) + def build_where(opts, other = []) case opts when String, Array - @klass.send(:sanitize_sql, other ? [opts, other] : opts) + @klass.send(:sanitize_sql, other.empty? ? opts : ([opts] + other)) when Hash attributes = @klass.send(:expand_hash_conditions_for_aggregates, opts) PredicateBuilder.new(table.engine).build_from_hash(attributes, table) -- cgit v1.2.3