Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Ensure hm:t#create/create! throws ActiveRecord::RecordNotSaved when t…

…he owner is new
  • Loading branch information...
commit 761283ffdb5750f8a38e2ed67891d2b2b9152d7f 1 parent 00544c7
@lifo lifo authored
View
4 activerecord/lib/active_record/associations/has_many_through_association.rb
@@ -8,6 +8,8 @@ class HasManyThroughAssociation < HasManyAssociation #:nodoc:
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 @@ def create!(attrs = nil)
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
View
7 activerecord/test/cases/associations/has_many_through_associations_test.rb
@@ -169,6 +169,13 @@ def test_associate_with_create_exclamation_and_no_options
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) }
Please sign in to comment.
Something went wrong with that request. Please try again.