aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2011-06-30 22:41:18 +0100
committerJon Leighton <j@jonathanleighton.com>2011-06-30 23:39:58 +0100
commit0e225ec583db523a8b7da332eaf689149ed60447 (patch)
treebb18c8a7b672eb7545c36daba2aba2c0a3ce46c0
parent6a283d598f243197375266c8b981b66f1b5f10c5 (diff)
downloadrails-0e225ec583db523a8b7da332eaf689149ed60447.tar.gz
rails-0e225ec583db523a8b7da332eaf689149ed60447.tar.bz2
rails-0e225ec583db523a8b7da332eaf689149ed60447.zip
Assign the association attributes to the associated record before the before_initialize callback of the record runs. Fixes #1842.
-rw-r--r--activerecord/CHANGELOG12
-rw-r--r--activerecord/lib/active_record/associations/association.rb9
-rw-r--r--activerecord/lib/active_record/associations/collection_association.rb6
-rw-r--r--activerecord/lib/active_record/associations/singular_association.rb3
-rw-r--r--activerecord/lib/active_record/reflection.rb4
-rw-r--r--activerecord/test/cases/associations/has_many_associations_test.rb7
-rw-r--r--activerecord/test/cases/associations/has_one_associations_test.rb7
-rw-r--r--activerecord/test/models/bulb.rb9
8 files changed, 45 insertions, 12 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index ead2a244e5..4113a16c12 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,17 @@
*Rails 3.1.0 (unreleased)*
+* ActiveRecord::MacroReflection::AssociationReflection#build_record has a new method signature.
+
+ Before: def build_association(*options)
+ After: def build_association(*options, &block)
+
+ Users who are redefining this method to extend functionality should ensure that the block is
+ passed through to ActiveRecord::Base#new.
+
+ This change is necessary to fix https://github.com/rails/rails/issues/1842.
+
+ [Jon Leighton]
+
* AR#pluralize_table_names can be used to singularize/pluralize table name of an individual model:
class User < ActiveRecord::Base
diff --git a/activerecord/lib/active_record/associations/association.rb b/activerecord/lib/active_record/associations/association.rb
index c3aee90124..daadc8aa81 100644
--- a/activerecord/lib/active_record/associations/association.rb
+++ b/activerecord/lib/active_record/associations/association.rb
@@ -224,6 +224,15 @@ module ActiveRecord
def association_class
@reflection.klass
end
+
+ def build_record(attributes, options)
+ reflection.build_association(attributes, options) do |record|
+ record.assign_attributes(
+ create_scope.except(*record.changed),
+ :without_protection => true
+ )
+ end
+ end
end
end
end
diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb
index 337a0d48f1..224e0095d9 100644
--- a/activerecord/lib/active_record/associations/collection_association.rb
+++ b/activerecord/lib/active_record/associations/collection_association.rb
@@ -423,12 +423,6 @@ module ActiveRecord
scoped.scope_for_create.stringify_keys
end
- def build_record(attributes, options)
- record = reflection.build_association(attributes, options)
- record.assign_attributes(create_scope.except(*record.changed), :without_protection => true)
- record
- end
-
def delete_or_destroy(records, method)
records = records.flatten
records.each { |record| raise_on_type_mismatch(record) }
diff --git a/activerecord/lib/active_record/associations/singular_association.rb b/activerecord/lib/active_record/associations/singular_association.rb
index ce1f2a5543..6b010064d5 100644
--- a/activerecord/lib/active_record/associations/singular_association.rb
+++ b/activerecord/lib/active_record/associations/singular_association.rb
@@ -26,8 +26,7 @@ module ActiveRecord
end
def build(attributes = {}, options = {})
- record = reflection.build_association(attributes, options)
- record.assign_attributes(create_scope.except(*record.changed), :without_protection => true)
+ record = build_record(attributes, options)
yield(record) if block_given?
set_new_record(record)
record
diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb
index 57023f5388..cfb41fd28c 100644
--- a/activerecord/lib/active_record/reflection.rb
+++ b/activerecord/lib/active_record/reflection.rb
@@ -175,8 +175,8 @@ module ActiveRecord
# Returns a new, unsaved instance of the associated class. +options+ will
# be passed to the class's constructor.
- def build_association(*options)
- klass.new(*options)
+ def build_association(*options, &block)
+ klass.new(*options, &block)
end
# Creates a new instance of the associated class, and immediately saves it
diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb
index 9af1f7249f..7e331658a3 100644
--- a/activerecord/test/cases/associations/has_many_associations_test.rb
+++ b/activerecord/test/cases/associations/has_many_associations_test.rb
@@ -1557,4 +1557,11 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
assert_equal 1, contract.hi_count
assert_equal 1, contract.bye_count
end
+
+ def test_association_attributes_are_available_to_after_initialize
+ car = Car.create(:name => 'honda')
+ bulb = car.bulbs.build
+
+ assert_equal car.id, bulb.attributes_after_initialize['car_id']
+ end
end
diff --git a/activerecord/test/cases/associations/has_one_associations_test.rb b/activerecord/test/cases/associations/has_one_associations_test.rb
index 299688d840..a1d0dafa57 100644
--- a/activerecord/test/cases/associations/has_one_associations_test.rb
+++ b/activerecord/test/cases/associations/has_one_associations_test.rb
@@ -438,4 +438,11 @@ class HasOneAssociationsTest < ActiveRecord::TestCase
bulb = car.create_bulb!{ |b| b.color = 'Red' }
assert_equal 'RED!', bulb.color
end
+
+ def test_association_attributes_are_available_to_after_initialize
+ car = Car.create(:name => 'honda')
+ bulb = car.create_bulb
+
+ assert_equal car.id, bulb.attributes_after_initialize['car_id']
+ end
end
diff --git a/activerecord/test/models/bulb.rb b/activerecord/test/models/bulb.rb
index 0dcc8d5970..efb98b66e7 100644
--- a/activerecord/test/models/bulb.rb
+++ b/activerecord/test/models/bulb.rb
@@ -4,13 +4,18 @@ class Bulb < ActiveRecord::Base
attr_protected :car_id, :frickinawesome
- attr_reader :scope_after_initialize
+ attr_reader :scope_after_initialize, :attributes_after_initialize
after_initialize :record_scope_after_initialize
def record_scope_after_initialize
@scope_after_initialize = self.class.scoped
end
+ after_initialize :record_attributes_after_initialize
+ def record_attributes_after_initialize
+ @attributes_after_initialize = attributes.dup
+ end
+
def color=(color)
self[:color] = color.upcase + "!"
end
@@ -28,4 +33,4 @@ class Bulb < ActiveRecord::Base
end
class CustomBulb < Bulb
-end \ No newline at end of file
+end