aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations/association_collection.rb
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2008-04-06 00:27:12 +0000
committerPratik Naik <pratiknaik@gmail.com>2008-04-06 00:27:12 +0000
commitf6b12c11cd3a6df8525dd16ec093ec473813489e (patch)
tree121421e0e9199655419cb2f4a29fa744c32bbb26 /activerecord/lib/active_record/associations/association_collection.rb
parent15d88885eedbac1193361a9eea957a7f49e39c9e (diff)
downloadrails-f6b12c11cd3a6df8525dd16ec093ec473813489e.tar.gz
rails-f6b12c11cd3a6df8525dd16ec093ec473813489e.tar.bz2
rails-f6b12c11cd3a6df8525dd16ec093ec473813489e.zip
Refactor HasManyThroughAssociation to inherit from HasManyAssociation. Association callbacks and <association>_ids= now work with hm:t. Closes #11516 [rubyruy]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9230 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record/associations/association_collection.rb')
-rw-r--r--activerecord/lib/active_record/associations/association_collection.rb16
1 files changed, 15 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb
index ce1c8a262d..73f22cb0fa 100644
--- a/activerecord/lib/active_record/associations/association_collection.rb
+++ b/activerecord/lib/active_record/associations/association_collection.rb
@@ -13,6 +13,14 @@ module ActiveRecord
@loaded = false
end
+ def build(attributes = {})
+ if attributes.is_a?(Array)
+ attributes.collect { |attr| build(attr) }
+ else
+ build_record(attributes) { |record| set_belongs_to_association_for(record) }
+ end
+ end
+
# Add +records+ to this association. Returns +self+ so method calls may be chained.
# Since << flattens its argument list and inserts each record, +push+ and +concat+ behave identically.
def <<(*records)
@@ -55,7 +63,13 @@ module ActiveRecord
def delete(*records)
records = flatten_deeper(records)
records.each { |record| raise_on_type_mismatch(record) }
- records.reject! { |record| @target.delete(record) if record.new_record? }
+ records.reject! do |record|
+ if record.new_record?
+ callback(:before_remove, record)
+ @target.delete(record)
+ callback(:after_remove, record)
+ end
+ end
return if records.empty?
@owner.transaction do