module ActiveRecordExtras::Relation::ClassMethods

Public Instance Methods

create_or_update(attributes, &block) click to toggle source
# File lib/monkey_patch_happy/active_record_extras.rb, line 7
def create_or_update(attributes, &block)
  new_or_assign(attributes) do |obj|
    block.call(obj) if block_given?
  end
end
new_or_assign(attributes) { |obj| ... } click to toggle source
# File lib/monkey_patch_happy/active_record_extras.rb, line 14
def new_or_assign(attributes)
  obj = where(attributes).last || new(attributes) #不存在就创建。
  # obj.assign_attributes(attributes)
  yield obj
  obj.updated_at = Time.new
  obj.save
end