提交 686418c6 编写于 作者: J Jon Leighton

Move create and create! next to build

上级 b93d2189
......@@ -66,6 +66,27 @@ def build(attributes = {}, &block)
end
end
def create(attrs = {})
if attrs.is_a?(Array)
attrs.collect { |attr| create(attr) }
else
ensure_owner_is_persisted!
transaction do
build_record(attrs) do |record|
yield(record) if block_given?
insert_record(record)
end
end
end
end
def create!(attrs = {}, &block)
record = create(attrs, &block)
Array.wrap(record).each(&:save!)
record
end
# Add +records+ to this association. Returns +self+ so method calls may be chained.
# Since << flattens its argument list and inserts each record, +push+ and +concat+ behave identically.
def <<(*records)
......@@ -191,27 +212,6 @@ def destroy(*records)
delete_or_destroy(records, :destroy)
end
def create(attrs = {})
if attrs.is_a?(Array)
attrs.collect { |attr| create(attr) }
else
ensure_owner_is_persisted!
transaction do
build_record(attrs) do |record|
yield(record) if block_given?
insert_record(record)
end
end
end
end
def create!(attrs = {}, &block)
record = create(attrs, &block)
Array.wrap(record).each(&:save!)
record
end
# Returns the size of the collection by executing a SELECT COUNT(*)
# query if the collection hasn't been loaded, and calling
# <tt>collection.size</tt> if it has.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册