From 8df723ec8ba14fd2a7738474cd580daac8afb209 Mon Sep 17 00:00:00 2001 From: Michael Trim Date: Mon, 23 Jun 2008 21:00:35 +0100 Subject: [PATCH] Allow block to be passed to collection.build With a has_many association, it is possible to pass a block to the collection.create method, which is yielded with the newly created object to allow additional attributes to be set. This patch adds the same functionality to the collection.build method. Signed-off-by: Michael Trim --- .../associations/association_collection.rb | 5 ++++- 1 files changed, 4 insertions(+), 1 deletions(-) diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index 52d2a98..eff185b 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -82,7 +82,10 @@ module ActiveRecord if attributes.is_a?(Array) attributes.collect { |attr| build(attr) } else - build_record(attributes) { |record| set_belongs_to_association_for(record) } + build_record(attributes) do |record| + set_belongs_to_association_for(record) + yield(record) if block_given? + end end end -- 1.5.4.3