aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2012-03-05 17:00:48 -0300
committerRafael Mendonça França <rafaelmfranca@gmail.com>2012-03-07 21:07:29 -0300
commitc9c7ee7710a637e1dd3c1d4be3960fe22f8ee3d1 (patch)
treea6c0cea59581865d7d8e4b7c2fa07117ccf6e61a
parente9bf0e3157fcc15f3520559a34bbfdd894dd8112 (diff)
downloadrails-c9c7ee7710a637e1dd3c1d4be3960fe22f8ee3d1.tar.gz
rails-c9c7ee7710a637e1dd3c1d4be3960fe22f8ee3d1.tar.bz2
rails-c9c7ee7710a637e1dd3c1d4be3960fe22f8ee3d1.zip
Not need to pass join attributes to association build
-rw-r--r--activerecord/lib/active_record/associations/has_many_through_association.rb4
-rw-r--r--activerecord/lib/active_record/associations/through_association.rb10
-rw-r--r--activerecord/test/cases/associations/has_many_through_associations_test.rb5
-rw-r--r--activerecord/test/models/reader.rb9
-rw-r--r--activerecord/test/models/secure_reader.rb9
5 files changed, 21 insertions, 16 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 9657cb081d..53d49fef2e 100644
--- a/activerecord/lib/active_record/associations/has_many_through_association.rb
+++ b/activerecord/lib/active_record/associations/has_many_through_association.rb
@@ -73,7 +73,9 @@ module ActiveRecord
# association
def build_through_record(record)
@through_records[record.object_id] ||= begin
- through_record = through_association.build(construct_join_attributes(record))
+ ensure_mutable
+
+ through_record = through_association.build
through_record.send("#{source_reflection.name}=", record)
through_record
end
diff --git a/activerecord/lib/active_record/associations/through_association.rb b/activerecord/lib/active_record/associations/through_association.rb
index f95e5337c2..fd0e90aaf0 100644
--- a/activerecord/lib/active_record/associations/through_association.rb
+++ b/activerecord/lib/active_record/associations/through_association.rb
@@ -37,9 +37,7 @@ module ActiveRecord
# situation it is more natural for the user to just create or modify their join records
# directly as required.
def construct_join_attributes(*records)
- if source_reflection.macro != :belongs_to
- raise HasManyThroughCantAssociateThroughHasOneOrManyReflection.new(owner, reflection)
- end
+ ensure_mutable
join_attributes = {
source_reflection.foreign_key =>
@@ -73,6 +71,12 @@ module ActiveRecord
!owner[through_reflection.foreign_key].nil?
end
+ def ensure_mutable
+ if source_reflection.macro != :belongs_to
+ raise HasManyThroughCantAssociateThroughHasOneOrManyReflection.new(owner, reflection)
+ end
+ end
+
def ensure_not_nested
if reflection.nested?
raise HasManyThroughNestedAssociationsAreReadonly.new(owner, reflection)
diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb
index 8e64fd7c70..198e537dc0 100644
--- a/activerecord/test/cases/associations/has_many_through_associations_test.rb
+++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb
@@ -4,7 +4,6 @@ require 'models/person'
require 'models/reference'
require 'models/job'
require 'models/reader'
-require 'models/secure_reader'
require 'models/comment'
require 'models/tag'
require 'models/tagging'
@@ -60,7 +59,7 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase
end
def test_associate_existing_with_strict_mass_assignment_sanitizer
- ActiveRecord::Base.mass_assignment_sanitizer = :strict
+ SecureReader.mass_assignment_sanitizer = :strict
SecureReader.new
@@ -71,7 +70,7 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase
post.secure_people << person
end
ensure
- ActiveRecord::Base.mass_assignment_sanitizer = :logger
+ SecureReader.mass_assignment_sanitizer = :logger
end
def test_associate_existing_record_twice_should_add_to_target_twice
diff --git a/activerecord/test/models/reader.rb b/activerecord/test/models/reader.rb
index 0207a2bd92..59005ac604 100644
--- a/activerecord/test/models/reader.rb
+++ b/activerecord/test/models/reader.rb
@@ -3,3 +3,12 @@ class Reader < ActiveRecord::Base
belongs_to :person, :inverse_of => :readers
belongs_to :single_person, :class_name => 'Person', :foreign_key => :person_id, :inverse_of => :reader
end
+
+class SecureReader < ActiveRecord::Base
+ self.table_name = "readers"
+
+ belongs_to :secure_post, :class_name => "Post", :foreign_key => "post_id"
+ belongs_to :secure_person, :inverse_of => :secure_readers, :class_name => "Person", :foreign_key => "person_id"
+
+ attr_accessible nil
+end
diff --git a/activerecord/test/models/secure_reader.rb b/activerecord/test/models/secure_reader.rb
deleted file mode 100644
index 3a2a8496fd..0000000000
--- a/activerecord/test/models/secure_reader.rb
+++ /dev/null
@@ -1,9 +0,0 @@
-class SecureReader < ActiveRecord::Base
- self.table_name = "readers"
-
- belongs_to :secure_post, :class_name => "Post", :foreign_key => "post_id"
- belongs_to :secure_person, :inverse_of => :secure_readers, :class_name => "Person", :foreign_key => "person_id"
-
-
- attr_accessible nil
-end