Permalink
Browse files

Merge pull request #7661 from ernie/build-join-records-on-unsaved-hmt

Fix collection= on hm:t join models when unsaved
  • Loading branch information...
1 parent 65d7076 commit ff635b73de1b5ca945342bd4aef33593e6d0c1cd @rafaelfranca rafaelfranca committed with sikachu Sep 17, 2012
@@ -1,5 +1,11 @@
## Rails 3.2.9 (unreleased)
+* Fix creation of through association models when using `collection=[]`
+ on a `has_many :through` association from an unsaved model.
+ Fix #7661.
+
+ *Ernie Miller*
+
* Explain only normal CRUD sql (select / update / insert / delete).
Fix problem that explains unexplainable sql. Closes #7544 #6458.
@@ -38,6 +38,20 @@ def concat(*records)
super
end
+ def concat_records(records)
+ ensure_not_nested
+
+ records = super
+
+ if owner.new_record? && records
+ records.flatten.each do |record|
+ build_through_record(record)
+ end
+ end
+
+ records
+ end
+
def insert_record(record, validate = true, raise = false)
ensure_not_nested
@@ -845,6 +845,11 @@ def test_save_should_not_raise_exception_when_join_record_has_errors
end
end
+ def test_assign_array_to_new_record_builds_join_records
+ c = Category.new(:name => 'Fishing', :authors => [Author.first])
+ assert_equal 1, c.categorizations.size
+ end
+
def test_create_bang_should_raise_exception_when_join_record_has_errors
repair_validations(Categorization) do
Categorization.validate { |r| r.errors[:base] << 'Invalid Categorization' }

0 comments on commit ff635b7

Please sign in to comment.