Skip to content

Commit

Permalink
has_many :through create should not raise validation errors
Browse files Browse the repository at this point in the history
[#2934 state:committed]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
  • Loading branch information
railsbob authored and jeremy committed Aug 10, 2009
1 parent e4ceea3 commit e06a0b0
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 4 deletions.
Expand Up @@ -20,7 +20,11 @@ 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 = attrs ? @reflection.klass.send(:with_scope, :create => attrs) { @reflection.create_association } : @reflection.create_association
raise_on_type_mismatch(object)
add_record_to_target_with_callbacks(object) do |r|
insert_record(object, false)
end
object
end
end
Expand Down
Expand Up @@ -11,9 +11,12 @@
require 'models/owner'
require 'models/pet'
require 'models/toy'
require 'models/contract'
require 'models/company'
require 'models/developer'

class HasManyThroughAssociationsTest < ActiveRecord::TestCase
fixtures :posts, :readers, :people, :comments, :authors, :owners, :pets, :toys, :jobs, :references
fixtures :posts, :readers, :people, :comments, :authors, :owners, :pets, :toys, :jobs, :references, :companies

def test_associate_existing
assert_queries(2) { posts(:thinking);people(:david) }
Expand Down Expand Up @@ -176,6 +179,30 @@ def test_create_on_new_record
assert_raises(ActiveRecord::RecordNotSaved) { p.people.create!(:first_name => "snow") }
end

def test_associate_with_create_and_invalid_options
peeps = companies(:first_firm).developers.count
assert_nothing_raised { companies(:first_firm).developers.create(:name => '0') }
assert_equal peeps, companies(:first_firm).developers.count
end

def test_associate_with_create_and_valid_options
peeps = companies(:first_firm).developers.count
assert_nothing_raised { companies(:first_firm).developers.create(:name => 'developer') }
assert_equal peeps + 1, companies(:first_firm).developers.count
end

def test_associate_with_create_bang_and_invalid_options
peeps = companies(:first_firm).developers.count
assert_raises(ActiveRecord::RecordInvalid) { companies(:first_firm).developers.create!(:name => '0') }
assert_equal peeps, companies(:first_firm).developers.count
end

def test_associate_with_create_bang_and_valid_options
peeps = companies(:first_firm).developers.count
assert_nothing_raised { companies(:first_firm).developers.create!(:name => 'developer') }
assert_equal peeps + 1, companies(:first_firm).developers.count
end

def test_clear_associations
assert_queries(2) { posts(:welcome);posts(:welcome).people(true) }

Expand Down
4 changes: 2 additions & 2 deletions activerecord/test/cases/reflection_test.rb
Expand Up @@ -176,8 +176,8 @@ def test_association_reflection_in_modules

def test_reflection_of_all_associations
# FIXME these assertions bust a lot
assert_equal 29, Firm.reflect_on_all_associations.size
assert_equal 22, Firm.reflect_on_all_associations(:has_many).size
assert_equal 31, Firm.reflect_on_all_associations.size
assert_equal 24, Firm.reflect_on_all_associations(:has_many).size
assert_equal 7, Firm.reflect_on_all_associations(:has_one).size
assert_equal 0, Firm.reflect_on_all_associations(:belongs_to).size
end
Expand Down
2 changes: 2 additions & 0 deletions activerecord/test/models/company.rb
Expand Up @@ -9,6 +9,8 @@ class Company < AbstractCompany
validates_presence_of :name

has_one :dummy_account, :foreign_key => "firm_id", :class_name => "Account"
has_many :contracts
has_many :developers, :through => :contracts

def arbitrary_method
"I am Jack's profound disappointment"
Expand Down
4 changes: 4 additions & 0 deletions activerecord/test/models/contract.rb
@@ -0,0 +1,4 @@
class Contract < ActiveRecord::Base
belongs_to :company
belongs_to :developer
end
4 changes: 4 additions & 0 deletions activerecord/test/schema/schema.rb
Expand Up @@ -131,6 +131,10 @@ def create_table(*args, &block)
t.integer :extendedWarranty, :null => false
end

create_table :contracts, :force => true do |t|
t.integer :developer_id
t.integer :company_id
end

create_table :customers, :force => true do |t|
t.string :name
Expand Down

0 comments on commit e06a0b0

Please sign in to comment.