Skip to content

Instantly share code, notes, and snippets.

@fmundaca
Created June 1, 2018 16:02
Show Gist options
  • Save fmundaca/3a2185751d456b709825b9ca11b98426 to your computer and use it in GitHub Desktop.
Save fmundaca/3a2185751d456b709825b9ca11b98426 to your computer and use it in GitHub Desktop.
#begin @eolivar RKM-2780 07/03/2018
def create_or_update_optionals user, optionals
unless !optionals.present?
optionals = optionals.select { |k, v| (v[:value].nil? && v[:id].present?) || (v[:value].present? && !v[:value].empty?) }
optionals.each do |key, value|
user_optional = UserOptional.where(id: value[:id]).first unless !value[:id].present?
if user_optional.present? && value[:value].nil?
user_optional.destroy
next
end
unless !value[:value].present?
user_optional = user.user_optionals.find_or_create_by(optional_id: value[:optional_id]) unless user_optional.present?
user_optional.value = value[:value]
user_optional.save
end
end
end
end
#end @eolivar RKM-2780 07/03/2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment