Module: ActiveRecord::Extensions::CreateAndUpdate::ClassMethods

Defined in:
lib/ar-extensions/create_and_update.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#create_with_extension(attributes = nil, options = {}, &block) ⇒ Object

Creates an object, instantly saves it as a record (if the validation permits it), and returns it. If the save fails under validations, the unsaved object is still returned.



173
174
175
176
177
178
179
180
181
182
183
# File 'lib/ar-extensions/create_and_update.rb', line 173

def create_with_extension(attributes = nil, options={}, &block)#:nodoc:
  return create_without_extension(attributes, &block) unless options.any?
  if attributes.is_a?(Array)
    attributes.collect { |attr| create(attr, &block) }
  else
    object = new(attributes)
    yield(object) if block_given?
    object.save(options)
    object
  end
end

#create_with_extension!(attributes = nil, options = {}, &block) ⇒ Object

Creates an object just like Base.create but calls save! instead of save so an exception is raised if the record is invalid.



187
188
189
190
# File 'lib/ar-extensions/create_and_update.rb', line 187

def create_with_extension!(attributes = nil, options={}, &block)#:nodoc:
  return create_without_extension!(attributes, &block) unless options.any?
  create_with_extension(attributes, options.merge(:raise_exception => true), &block)
end