提交 dfec3737 编写于 作者: J Jon Leighton

Ensure that the foreign key gets set when doing record.create_association or...

Ensure that the foreign key gets set when doing record.create_association or record.create_association. Fixes #1960.
上级 c1c886a0
......@@ -18,11 +18,11 @@ def writer(record)
end
def create(attributes = {}, options = {}, &block)
build(attributes, options, &block).tap { |record| record.save }
create_record(attributes, options, &block)
end
def create!(attributes = {}, options = {}, &block)
build(attributes, options, &block).tap { |record| record.save! }
create_record(attributes, options, true, &block)
end
def build(attributes = {}, options = {})
......@@ -50,6 +50,15 @@ def replace(record)
def set_new_record(record)
replace(record)
end
def create_record(attributes, options, raise_error = false)
record = build_record(attributes, options)
yield(record) if block_given?
saved = record.save
set_new_record(record)
raise RecordInvalid.new(record) if !saved && raise_error
record
end
end
end
end
......@@ -667,4 +667,18 @@ def test_create_bang_with_block
firm = client.create_firm!{ |f| f.name = 'Agency Company' }
assert_equal 'Agency Company', firm.name
end
def test_should_set_foreign_key_on_create_association
client = Client.create! :name => "fuu"
firm = client.create_firm :name => "baa"
assert_equal firm.id, client.client_of
end
def test_should_set_foreign_key_on_create_association!
client = Client.create! :name => "fuu"
firm = client.create_firm! :name => "baa"
assert_equal firm.id, client.client_of
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册