class SelfCollectStation def self.update_or_create(params) station = detect_or_initialize_by(extension_id: attributes[:extension_id]) if station.persisted? station.update(attributes) else station.save end end def self.detect_or_initialize_by(params) if loaded? detect { |record| record.extension_id == params[:extension_id] } || new(params) else find_or_initialize_by(params) end end end