diff options
Diffstat (limited to 'activerecord/lib/active_record/associations')
3 files changed, 35 insertions, 16 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 b26ea586e5..e08bd04ebb 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 @@ -33,10 +33,10 @@ module ActiveRecord if ids.size == 1 id = ids.first.to_i - record = load_target.detect { |record| id == record.id } + record = load_target.detect { |r| id == r.id } expects_array ? [record] : record else - load_target.select { |record| ids.include?(record.id) } + load_target.select { |r| ids.include?(r.id) } end else conditions = "#{@finder_sql}" @@ -84,19 +84,19 @@ module ActiveRecord else columns = @owner.connection.columns(@reflection.options[:join_table], "#{@reflection.options[:join_table]} Columns") - attributes = columns.inject({}) do |attributes, column| + attributes = columns.inject({}) do |attrs, column| case column.name when @reflection.primary_key_name - attributes[column.name] = @owner.quoted_id + attrs[column.name] = @owner.quoted_id when @reflection.association_foreign_key - attributes[column.name] = record.quoted_id + attrs[column.name] = record.quoted_id else - if record.attributes.has_key?(column.name) + if record.has_attribute?(column.name) value = @owner.send(:quote_value, record[column.name], column) - attributes[column.name] = value unless value.nil? + attrs[column.name] = value unless value.nil? end end - attributes + attrs end sql = diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb index ead447baa0..f134ae0757 100644 --- a/activerecord/lib/active_record/associations/has_many_association.rb +++ b/activerecord/lib/active_record/associations/has_many_association.rb @@ -41,10 +41,10 @@ module ActiveRecord if ids.size == 1 id = ids.first - record = load_target.detect { |record| id == record.id } + record = load_target.detect { |r| id == r.id } expects_array ? [ record ] : record else - load_target.select { |record| ids.include?(record.id) } + load_target.select { |r| ids.include?(r.id) } end else conditions = "#{@finder_sql}" diff --git a/activerecord/lib/active_record/associations/has_many_through_association.rb b/activerecord/lib/active_record/associations/has_many_through_association.rb index 14f752abe4..883375707d 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -262,12 +262,31 @@ module ActiveRecord end def conditions - @conditions ||= [ - (interpolate_sql(@reflection.klass.send(:sanitize_sql, @reflection.options[:conditions])) if @reflection.options[:conditions]), - (interpolate_sql(@reflection.active_record.send(:sanitize_sql, @reflection.through_reflection.options[:conditions])) if @reflection.through_reflection.options[:conditions]), - (interpolate_sql(@reflection.active_record.send(:sanitize_sql, @reflection.source_reflection.options[:conditions])) if @reflection.source_reflection.options[:conditions]), - ("#{@reflection.through_reflection.table_name}.#{@reflection.through_reflection.klass.inheritance_column} = #{@reflection.klass.quote_value(@reflection.through_reflection.klass.name.demodulize)}" unless @reflection.through_reflection.klass.descends_from_active_record?) - ].compact.collect { |condition| "(#{condition})" }.join(' AND ') unless (!@reflection.options[:conditions] && !@reflection.through_reflection.options[:conditions] && !@reflection.source_reflection.options[:conditions] && @reflection.through_reflection.klass.descends_from_active_record?) + @conditions = build_conditions unless defined?(@conditions) + @conditions + end + + def build_conditions + association_conditions = @reflection.options[:conditions] + through_conditions = @reflection.through_reflection.options[:conditions] + source_conditions = @reflection.source_reflection.options[:conditions] + uses_sti = !@reflection.through_reflection.klass.descends_from_active_record? + + if association_conditions || through_conditions || source_conditions || uses_sti + all = [] + + [association_conditions, through_conditions, source_conditions].each do |conditions| + all << interpolate_sql(sanitize_sql(conditions)) if conditions + end + + all << build_sti_condition if uses_sti + + all.map { |sql| "(#{sql})" } * ' AND ' + end + end + + def build_sti_condition + "#{@reflection.through_reflection.table_name}.#{@reflection.through_reflection.klass.inheritance_column} = #{@reflection.klass.quote_value(@reflection.through_reflection.klass.name.demodulize)}" end alias_method :sql_conditions, :conditions |