Skip to content
This repository
Browse code

Fixed that :delete_sql in has_and_belongs_to_many associations couldn…

…'t access record properties #1299 [Rick Olson]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1313 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 190e04645bf7ae873d922064783a71a88e0060fd 1 parent b3fcf2f
David Heinemeier Hansson dhh authored
2  activerecord/CHANGELOG
... ... @@ -1,5 +1,7 @@
1 1 *SVN*
2 2
  3 +* Fixed that :delete_sql in has_and_belongs_to_many associations couldn't access record properties #1299 [Rick Olson]
  4 +
3 5 * Fixed that clone would break when an aggregate had the same name as one of its attributes #1307 [bitsweat]
4 6
5 7 * Changed that destroying an object will only freeze the attributes hash, which keeps the object from having attributes changed (as that wouldn't make sense), but allows for the querying of associations after it has been destroyed.
2  activerecord/lib/active_record/associations.rb
@@ -506,6 +506,8 @@ def belongs_to(association_id, options = {})
506 506 # has_and_belongs_to_many :projects
507 507 # has_and_belongs_to_many :nations, :class_name => "Country"
508 508 # has_and_belongs_to_many :categories, :join_table => "prods_cats"
  509 + # has_and_belongs_to_many :active_projects, :join_table => 'developers_projects', :delete_sql =>
  510 + # 'DELETE FROM developers_projects WHERE active=1 AND developer_id = #{id} AND project_id = #{record.id}'
509 511 def has_and_belongs_to_many(association_id, options = {})
510 512 validate_options([ :class_name, :table_name, :foreign_key, :association_foreign_key, :conditions,
511 513 :join_table, :finder_sql, :delete_sql, :insert_sql, :order, :uniq ], options.keys)
4 activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
@@ -136,7 +136,7 @@ def insert_record(record)
136 136
137 137 def delete_records(records)
138 138 if sql = @options[:delete_sql]
139   - records.each { |record| @owner.connection.execute(sql) }
  139 + records.each { |record| @owner.connection.execute(interpolate_sql(sql, record)) }
140 140 else
141 141 ids = quoted_record_ids(records)
142 142 sql = "DELETE FROM #{@join_table} WHERE #{@association_class_primary_key_name} = #{@owner.quoted_id} AND #{@association_foreign_key} IN (#{ids})"
@@ -145,7 +145,7 @@ def delete_records(records)
145 145 end
146 146
147 147 def construct_sql
148   - interpolate_sql_options!(@options, :finder_sql, :delete_sql)
  148 + interpolate_sql_options!(@options, :finder_sql)
149 149
150 150 if @options[:finder_sql]
151 151 @finder_sql = @options[:finder_sql]
19 activerecord/test/associations_test.rb
@@ -859,6 +859,25 @@ def test_deleting_array
859 859 assert_equal 0, david.projects(true).size
860 860 end
861 861
  862 + def test_deleting_with_sql
  863 + david = Developer.find(1)
  864 + active_record = Project.find(1)
  865 + active_record.developers.reload
  866 + assert_equal 2, active_record.developers_by_sql.size
  867 +
  868 + active_record.developers_by_sql.delete(david)
  869 + assert_equal 1, active_record.developers_by_sql(true).size
  870 + end
  871 +
  872 + def test_deleting_array_with_sql
  873 + active_record = Project.find(1)
  874 + active_record.developers.reload
  875 + assert_equal 2, active_record.developers_by_sql.size
  876 +
  877 + active_record.developers_by_sql.delete(Developer.find_all)
  878 + assert_equal 0, active_record.developers_by_sql(true).size
  879 + end
  880 +
862 881 def test_deleting_all
863 882 david = Developer.find(1)
864 883 david.projects.reload
1  activerecord/test/fixtures/project.rb
... ... @@ -1,6 +1,7 @@
1 1 class Project < ActiveRecord::Base
2 2 has_and_belongs_to_many :developers, :uniq => true
3 3 has_and_belongs_to_many :developers_named_david, :class_name => "Developer", :conditions => "name = 'David'", :uniq => true
  4 + has_and_belongs_to_many :developers_by_sql, :class_name => "Developer", :delete_sql => "DELETE FROM developers_projects WHERE project_id = \#{id} AND developer_id = \#{record.id}"
4 5 end
5 6
6 7 class SpecialProject < Project

0 comments on commit 190e046

Please sign in to comment.
Something went wrong with that request. Please try again.