method

find_or_instantiator_by_attributes

Importance_0
v3.0.0 - Show latest stable - 0 notes - Class: ActiveRecord::FinderMethods
find_or_instantiator_by_attributes(match, attributes, *args) protected

No documentation

This method has no description. You can help the Ruby on Rails community by adding new notes.

Hide source
# File activerecord/lib/active_record/relation/finder_methods.rb, line 243
    def find_or_instantiator_by_attributes(match, attributes, *args)
      protected_attributes_for_create, unprotected_attributes_for_create = {}, {}
      args.each_with_index do |arg, i|
        if arg.is_a?(Hash)
          protected_attributes_for_create = args[i].with_indifferent_access
        else
          unprotected_attributes_for_create[attributes[i]] = args[i]
        end
      end

      conditions = (protected_attributes_for_create.merge(unprotected_attributes_for_create)).slice(*attributes).symbolize_keys

      record = where(conditions).first

      unless record
        record = @klass.new do |r|
          r.send(:attributes=, protected_attributes_for_create, true) unless protected_attributes_for_create.empty?
          r.send(:attributes=, unprotected_attributes_for_create, false) unless unprotected_attributes_for_create.empty?
        end
        yield(record) if block_given?
        record.save if match.instantiator == :create
      end

      record
    end
Register or log in to add new notes.