diff options
author | Andrew White <andyw@pixeltrix.co.uk> | 2011-05-17 22:15:47 +0100 |
---|---|---|
committer | Andrew White <andyw@pixeltrix.co.uk> | 2011-05-17 22:16:24 +0100 |
commit | 3773aa4fbba86184a1f45feebaf72ed4f2702fe7 (patch) | |
tree | b56d9dc07d96e4d085667a98f53c3878509412c0 /activerecord/lib | |
parent | be199a1d53f9ceae5cfaac185bfd0d9c2a28f9d4 (diff) | |
download | rails-3773aa4fbba86184a1f45feebaf72ed4f2702fe7.tar.gz rails-3773aa4fbba86184a1f45feebaf72ed4f2702fe7.tar.bz2 rails-3773aa4fbba86184a1f45feebaf72ed4f2702fe7.zip |
Add block setting of attributes to singular associations
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/associations/builder/singular_association.rb | 12 | ||||
-rw-r--r-- | activerecord/lib/active_record/associations/singular_association.rb | 11 |
2 files changed, 12 insertions, 11 deletions
diff --git a/activerecord/lib/active_record/associations/builder/singular_association.rb b/activerecord/lib/active_record/associations/builder/singular_association.rb index 62d48d3a2c..638a2ec72a 100644 --- a/activerecord/lib/active_record/associations/builder/singular_association.rb +++ b/activerecord/lib/active_record/associations/builder/singular_association.rb @@ -29,16 +29,16 @@ module ActiveRecord::Associations::Builder def define_constructors name = self.name - model.redefine_method("build_#{name}") do |*params| - association(name).build(*params) + model.redefine_method("build_#{name}") do |*params, &block| + association(name).build(*params, &block) end - model.redefine_method("create_#{name}") do |*params| - association(name).create(*params) + model.redefine_method("create_#{name}") do |*params, &block| + association(name).create(*params, &block) end - model.redefine_method("create_#{name}!") do |*params| - association(name).create!(*params) + model.redefine_method("create_#{name}!") do |*params, &block| + association(name).create!(*params, &block) end end end diff --git a/activerecord/lib/active_record/associations/singular_association.rb b/activerecord/lib/active_record/associations/singular_association.rb index 44dbe984d4..68fe0bde76 100644 --- a/activerecord/lib/active_record/associations/singular_association.rb +++ b/activerecord/lib/active_record/associations/singular_association.rb @@ -17,17 +17,18 @@ module ActiveRecord replace(record) end - def create(attributes = {}, options = {}) - build(attributes, options).tap { |record| record.save } + def create(attributes = {}, options = {}, &block) + build(attributes, options, &block).tap { |record| record.save } end - def create!(attributes = {}, options = {}) - build(attributes, options).tap { |record| record.save! } + def create!(attributes = {}, options = {}, &block) + build(attributes, options, &block).tap { |record| record.save! } end - def build(attributes = {}, options = {}) + def build(attributes = {}, options = {}, &block) record = reflection.build_association(attributes, options) record.assign_attributes(create_scope.except(*record.changed), :without_protection => true) + yield(record) if block_given? set_new_record(record) record end |