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

best solution?

Signed-off-by: Steven Soroka <ssoroka78@gmail.com>
This commit is contained in:
Steven Soroka 2008-05-01 14:09:12 -05:00 committed by David Heinemeier Hansson
parent c353794dff
commit 9c20391bbe
2 changed files with 14 additions and 2 deletions

View file

@ -10,14 +10,14 @@ module ActiveRecord
def create!(attrs = nil)
@reflection.klass.transaction do
self << (object = @reflection.klass.send(:with_scope, :create => attrs) { @reflection.klass.create! })
self << (object = attrs ? @reflection.klass.send(:with_scope, :create => attrs) { @reflection.klass.create! } : @reflection.klass.create!)
object
end
end
def create(attrs = nil)
@reflection.klass.transaction do
self << (object = @reflection.klass.send(:with_scope, :create => attrs) { @reflection.klass.create })
self << (object = attrs ? @reflection.klass.send(:with_scope, :create => attrs) { @reflection.klass.create } : @reflection.klass.create)
object
end
end

View file

@ -115,6 +115,18 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase
assert posts(:thinking).reload.people(true).collect(&:first_name).include?("Jeb")
end
def test_associate_with_create_and_no_options
peeps = posts(:thinking).people.count
posts(:thinking).people.create
assert_equal peeps + 1, posts(:thinking).people.count
end
def test_associate_with_create_exclaimation_and_no_options
peeps = posts(:thinking).people.count
posts(:thinking).people.create!
assert_equal peeps + 1, posts(:thinking).people.count
end
def test_clear_associations
assert_queries(2) { posts(:welcome);posts(:welcome).people(true) }