diff options
author | Rick Olson <technoweenie@gmail.com> | 2006-10-09 03:21:52 +0000 |
---|---|---|
committer | Rick Olson <technoweenie@gmail.com> | 2006-10-09 03:21:52 +0000 |
commit | 850087f0bd691568690ca9d3169c88c0f3ad7c3b (patch) | |
tree | 4449040af40136b23389bb0a55ea748d1c80af1c /activerecord/lib | |
parent | 4e66f5bfe79fc9a41575eecdd687367dd2b9d3b1 (diff) | |
download | rails-850087f0bd691568690ca9d3169c88c0f3ad7c3b.tar.gz rails-850087f0bd691568690ca9d3169c88c0f3ad7c3b.tar.bz2 rails-850087f0bd691568690ca9d3169c88c0f3ad7c3b.zip |
Add #delete support to has_many :through associations. Closes #6049 [Martin Landers]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5267 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/associations/has_many_through_association.rb | 18 |
1 files changed, 18 insertions, 0 deletions
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 d7ca7b5834..cbc5616aa8 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -69,6 +69,24 @@ module ActiveRecord [:push, :concat].each { |method| alias_method method, :<< } + # Remove +records+ from this association. Does not destroy +records+. + def delete(*records) + records = flatten_deeper(records) + records.each { |associate| raise_on_type_mismatch(associate) } + records.reject! { |associate| @target.delete(associate) if associate.new_record? } + return if records.empty? + + @delete_join_finder ||= "find_all_by_#{@reflection.source_reflection.association_foreign_key}" + through = @reflection.through_reflection + through.klass.transaction do + records.each do |associate| + joins = @owner.send(through.name).send(@delete_join_finder, associate.id) + @owner.send(through.name).delete(joins) + @target.delete(associate) + end + end + end + def build(attrs = nil) raise ActiveRecord::HasManyThroughCantAssociateNewRecords.new(@owner, @reflection.through_reflection) end |