mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
Ensure hm:t#create/create! throws ActiveRecord::RecordNotSaved when the owner is new
This commit is contained in:
@@ -8,6 +8,8 @@ module ActiveRecord
|
||||
alias_method :new, :build
|
||||
|
||||
def create!(attrs = nil)
|
||||
ensure_owner_is_not_new
|
||||
|
||||
transaction do
|
||||
self << (object = attrs ? @reflection.klass.send(:with_scope, :create => attrs) { @reflection.create_association! } : @reflection.create_association!)
|
||||
object
|
||||
@@ -15,6 +17,8 @@ module ActiveRecord
|
||||
end
|
||||
|
||||
def create(attrs = nil)
|
||||
ensure_owner_is_not_new
|
||||
|
||||
transaction do
|
||||
self << (object = attrs ? @reflection.klass.send(:with_scope, :create => attrs) { @reflection.create_association } : @reflection.create_association)
|
||||
object
|
||||
|
||||
@@ -169,6 +169,13 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase
|
||||
assert_equal peeps + 1, posts(:thinking).people.count
|
||||
end
|
||||
|
||||
def test_create_on_new_record
|
||||
p = Post.new
|
||||
|
||||
assert_raises(ActiveRecord::RecordNotSaved) { p.people.create(:first_name => "mew") }
|
||||
assert_raises(ActiveRecord::RecordNotSaved) { p.people.create!(:first_name => "snow") }
|
||||
end
|
||||
|
||||
def test_clear_associations
|
||||
assert_queries(2) { posts(:welcome);posts(:welcome).people(true) }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user