提交 66280b9e 编写于 作者: J Jolyon Pawlyn

Allow a belonging to object to be created from a new record

If a 'has one' object is created from a new record, an ActiveRecord::RecordNotSaved error is raised but this behavior was also applied to the reverse scenario.
上级 24aeccb1
......@@ -107,6 +107,14 @@ def transaction_if(value)
yield
end
end
def _create_record(attributes, raise_error = false)
unless owner.persisted?
raise ActiveRecord::RecordNotSaved, "You cannot call create unless the parent is saved"
end
super
end
end
end
end
......@@ -63,10 +63,6 @@ def set_new_record(record)
end
def _create_record(attributes, raise_error = false)
unless owner.persisted?
raise ActiveRecord::RecordNotSaved, "You cannot call create unless the parent is saved"
end
record = build_record(attributes)
yield(record) if block_given?
saved = record.save
......
......@@ -272,6 +272,15 @@ def test_creating_the_belonging_object
assert_equal apple, citibank.firm
end
def test_creating_the_belonging_object_from_new_record
citibank = Account.new("credit_limit" => 10)
apple = citibank.create_firm("name" => "Apple")
assert_equal apple, citibank.firm
citibank.save
citibank.reload
assert_equal apple, citibank.firm
end
def test_creating_the_belonging_object_with_primary_key
client = Client.create(name: "Primary key client")
apple = client.create_firm_with_primary_key("name" => "Apple")
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册