aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2009-10-14 19:32:09 -0700
committerJeremy Kemper <jeremy@bitsweat.net>2009-10-14 19:32:09 -0700
commitc09a5ffea7780a0f940f3f963c892f0ae1316802 (patch)
treee5908025ddccf62dc183f6e6ced4d87bf265b28a /activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
parenta9f9ae385e2145b7f0d1ae43a7b66ed280b220b9 (diff)
parent6eee1dd62c85e23d84f16e8f300d8aba77bd5c64 (diff)
downloadrails-c09a5ffea7780a0f940f3f963c892f0ae1316802.tar.gz
rails-c09a5ffea7780a0f940f3f963c892f0ae1316802.tar.bz2
rails-c09a5ffea7780a0f940f3f963c892f0ae1316802.zip
Merge branch 'arel'
Diffstat (limited to 'activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb')
-rw-r--r--activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb22
1 files changed, 10 insertions, 12 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 417e2fdc0f..4672b0723e 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
@@ -34,7 +34,7 @@ module ActiveRecord
options[:readonly] = finding_with_ambiguous_select?(options[:select] || @reflection.options[:select])
options[:select] ||= (@reflection.options[:select] || '*')
end
-
+
def count_records
load_target.size
end
@@ -56,26 +56,23 @@ module ActiveRecord
if @reflection.options[:insert_sql]
@owner.connection.insert(interpolate_sql(@reflection.options[:insert_sql], record))
else
+ relation = arel_table(@reflection.options[:join_table])
attributes = columns.inject({}) do |attrs, column|
case column.name.to_s
when @reflection.primary_key_name.to_s
- attrs[column.name] = owner_quoted_id
+ attrs[relation[column.name]] = owner_quoted_id
when @reflection.association_foreign_key.to_s
- attrs[column.name] = record.quoted_id
+ attrs[relation[column.name]] = record.quoted_id
else
if record.has_attribute?(column.name)
value = @owner.send(:quote_value, record[column.name], column)
- attrs[column.name] = value unless value.nil?
+ attrs[relation[column.name]] = value unless value.nil?
end
end
attrs
end
- sql =
- "INSERT INTO #{@owner.connection.quote_table_name @reflection.options[:join_table]} (#{@owner.send(:quoted_column_names, attributes).join(', ')}) " +
- "VALUES (#{attributes.values.join(', ')})"
-
- @owner.connection.insert(sql)
+ relation.insert(attributes)
end
return true
@@ -85,9 +82,10 @@ module ActiveRecord
if sql = @reflection.options[:delete_sql]
records.each { |record| @owner.connection.delete(interpolate_sql(sql, record)) }
else
- ids = quoted_record_ids(records)
- sql = "DELETE FROM #{@owner.connection.quote_table_name @reflection.options[:join_table]} WHERE #{@reflection.primary_key_name} = #{owner_quoted_id} AND #{@reflection.association_foreign_key} IN (#{ids})"
- @owner.connection.delete(sql)
+ relation = arel_table(@reflection.options[:join_table])
+ relation.conditions(relation[@reflection.primary_key_name].eq(@owner.id).
+ and(Arel::In.new(relation[@reflection.association_foreign_key], records.map(&:id)))
+ ).delete
end
end