Module: Location::FindOrCreate

Included in:
City, District, State
Defined in:
app/models/concerns/find_or_create.rb

Instance Method Summary collapse

Instance Method Details

#find_or_save!(attributes) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'app/models/concerns/find_or_create.rb', line 3

def find_or_save!(attributes)
  attributes = self.attributes.select { |k| k =~ /_id$/ }
    .merge(attributes)

  if attributes[:normalized]
    object = self.class.find_by(attributes)
  end

  if object
    object
  else
    self.attributes = attributes
    save!
    self
  end
end