1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Properly create through records when called with where

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 #19073.
This commit is contained in:
Sean Griffin 2015-02-26 12:40:22 -07:00
parent f069b41321
commit 38218929e9
3 changed files with 26 additions and 0 deletions

View file

@ -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*

View file

@ -13,6 +13,19 @@ module ActiveRecord
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

View file

@ -595,6 +595,12 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase
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')