Skip to content

Commit

Permalink
Properly create through records when called with where
Browse files Browse the repository at this point in the history
Various behaviors needed by associations (such as creating the through
record) are lost when `where` is called, since we stop having a
`CollectionProxy` and start having an `AssociationRelation` which does
not contain this behavior. I *think* we should be able to rm
`AssociationRelation`, but we have tests saying the changes required to
do that would be bad (Without saying why. Of course. >_>)

Fixes rails#19073.
  • Loading branch information
sgrif committed Feb 26, 2015
1 parent f069b41 commit 3821892
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
7 changes: 7 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
* Correctly create through records when created on a has many through
association when using `where`.

Fixes #19073.

*Sean Griffin*

* Add `SchemaMigration.create_table` support any unicode charsets for MySQL.

*Ryuta Kamizono*
Expand Down
13 changes: 13 additions & 0 deletions activerecord/lib/active_record/association_relation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@ def ==(other)
other == to_a
end

def build(*args, &block)
scoping { @association.build(*args, &block) }
end
alias new build

def create(*args, &block)
scoping { @association.create(*args, &block) }
end

def create!(*args, &block)
scoping { @association.create!(*args, &block) }
end

private

def exec_queries
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,12 @@ def test_associate_with_create
assert posts(:thinking).reload.people(true).collect(&:first_name).include?("Jeb")
end

def test_through_record_is_built_when_created_with_where
assert_difference("posts(:thinking).readers.count", 1) do
posts(:thinking).people.where(first_name: "Jeb").create
end
end

def test_associate_with_create_and_no_options
peeps = posts(:thinking).people.count
posts(:thinking).people.create(:first_name => 'foo')
Expand Down

0 comments on commit 3821892

Please sign in to comment.