ruby 参加者DB级别20
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 参加者DB级别20相关的知识,希望对你有一定的参考价值。
event = Event.find(3935)
user = event.created_by
index = 0
errors = []
# file_path = '/home/ubuntu/3vectores.csv'
file_path = '/Users/imac/Documents/Inscrits SC Paris 131117.csv'
participant_type = AttendeeType.find(8729) #Participants
intervenants_type = AttendeeType.find(8730) #Intervenants
partenaires_type = AttendeeType.find(8731) #Partenaires
presse_type = AttendeeType.find(8732) #Presse
organisation_type = AttendeeType.find(8733) #Organisation
participant_type_custom_fields = {
"civilité" => participant_type.attendee_type_custom_fields.select {|field| field.name.downcase == "civilite"}.first.id,
"fonction" => participant_type.attendee_type_custom_fields.select {|field| field.name.downcase == "fonction"}.first.id,
"secteur d'activité" => participant_type.attendee_type_custom_fields.select {|field| field.name.downcase == "secteur d'activité"}.first.id,
"taille de société" => participant_type.attendee_type_custom_fields.select {|field| field.name.downcase == "taille de société"}.first.id,
"type de société" => participant_type.attendee_type_custom_fields.select {|field| field.name.downcase == "type de société"}.first.id,
"adresse" => participant_type.attendee_type_custom_fields.select {|field| field.name.downcase == "adresse"}.first.id,
"code postal" => participant_type.attendee_type_custom_fields.select {|field| field.name.downcase == "code postal"}.first.id,
"téléphone mobile" => participant_type.attendee_type_custom_fields.select {|field| field.name.downcase == "téléphone mobile"}.first.id,
"options d'adhésion" => participant_type.attendee_type_custom_fields.select {|field| field.name.downcase == "optins"}.first.id
}
intervenants_type_custom_fields = {
"civilité" => intervenants_type.attendee_type_custom_fields.select {|field| field.name.downcase == "civilite"}.first.id,
"fonction" => intervenants_type.attendee_type_custom_fields.select {|field| field.name.downcase == "fonction"}.first.id,
"secteur d'activité" => intervenants_type.attendee_type_custom_fields.select {|field| field.name.downcase == "secteur d'activité"}.first.id,
"taille de société" => intervenants_type.attendee_type_custom_fields.select {|field| field.name.downcase == "taille de société"}.first.id,
"type de société" => intervenants_type.attendee_type_custom_fields.select {|field| field.name.downcase == "type de société"}.first.id,
"adresse" => intervenants_type.attendee_type_custom_fields.select {|field| field.name.downcase == "adresse"}.first.id,
"code postal" => intervenants_type.attendee_type_custom_fields.select {|field| field.name.downcase == "code postal"}.first.id,
"téléphone mobile" => intervenants_type.attendee_type_custom_fields.select {|field| field.name.downcase == "téléphone mobile"}.first.id,
"options d'adhésion" => intervenants_type.attendee_type_custom_fields.select {|field| field.name.downcase == "optins"}.first.id
}
partenaires_type_custom_fields = {
"civilité" => partenaires_type.attendee_type_custom_fields.select {|field| field.name.downcase == "civilite"}.first.id,
"fonction" => partenaires_type.attendee_type_custom_fields.select {|field| field.name.downcase == "fonction"}.first.id,
"secteur d'activité" => partenaires_type.attendee_type_custom_fields.select {|field| field.name.downcase == "secteur d'activité"}.first.id,
"taille de société" => partenaires_type.attendee_type_custom_fields.select {|field| field.name.downcase == "taille de société"}.first.id,
"type de société" => partenaires_type.attendee_type_custom_fields.select {|field| field.name.downcase == "type de société"}.first.id,
"adresse" => partenaires_type.attendee_type_custom_fields.select {|field| field.name.downcase == "adresse"}.first.id,
"code postal" => partenaires_type.attendee_type_custom_fields.select {|field| field.name.downcase == "code postal"}.first.id,
"téléphone mobile" => partenaires_type.attendee_type_custom_fields.select {|field| field.name.downcase == "téléphone mobile"}.first.id,
"options d'adhésion" => partenaires_type.attendee_type_custom_fields.select {|field| field.name.downcase == "optins"}.first.id
}
presse_type_custom_fields = {
"civilité" => presse_type.attendee_type_custom_fields.select {|field| field.name.downcase == "civilite"}.first.id,
"fonction" => presse_type.attendee_type_custom_fields.select {|field| field.name.downcase == "fonction"}.first.id,
"secteur d'activité" => presse_type.attendee_type_custom_fields.select {|field| field.name.downcase == "secteur d'activité"}.first.id,
"taille de société" => presse_type.attendee_type_custom_fields.select {|field| field.name.downcase == "taille de société"}.first.id,
"type de société" => presse_type.attendee_type_custom_fields.select {|field| field.name.downcase == "type de société"}.first.id,
"adresse" => presse_type.attendee_type_custom_fields.select {|field| field.name.downcase == "adresse"}.first.id,
"code postal" => presse_type.attendee_type_custom_fields.select {|field| field.name.downcase == "code postal"}.first.id,
"téléphone mobile" => presse_type.attendee_type_custom_fields.select {|field| field.name.downcase == "téléphone mobile"}.first.id,
"options d'adhésion" => presse_type.attendee_type_custom_fields.select {|field| field.name.downcase == "optins"}.first.id
}
organisation_type_custom_fields = {
"civilité" => organisation_type.attendee_type_custom_fields.select {|field| field.name.downcase == "civilite"}.first.id,
"fonction" => organisation_type.attendee_type_custom_fields.select {|field| field.name.downcase == "fonction"}.first.id,
"secteur d'activité" => organisation_type.attendee_type_custom_fields.select {|field| field.name.downcase == "secteur d'activité"}.first.id,
"taille de société" => organisation_type.attendee_type_custom_fields.select {|field| field.name.downcase == "taille de société"}.first.id,
"type de société" => organisation_type.attendee_type_custom_fields.select {|field| field.name.downcase == "type de société"}.first.id,
"adresse" => organisation_type.attendee_type_custom_fields.select {|field| field.name.downcase == "adresse"}.first.id,
"code postal" => organisation_type.attendee_type_custom_fields.select {|field| field.name.downcase == "code postal"}.first.id,
"téléphone mobile" => organisation_type.attendee_type_custom_fields.select {|field| field.name.downcase == "téléphone mobile"}.first.id,
"options d'adhésion" => organisation_type.attendee_type_custom_fields.select {|field| field.name.downcase == "optins"}.first.id
}
super_hash = {
:participants => {
:type => participant_type,
:fields =>participant_type_custom_fields
},
:intervenants => {
:type => intervenants_type,
:fields =>intervenants_type_custom_fields
},
:partenaires => {
:type => partenaires_type,
:fields =>partenaires_type_custom_fields
},
:presse => {
:type => presse_type,
:fields =>presse_type_custom_fields
},
:organisation => {
:type => organisation_type,
:fields =>organisation_type_custom_fields
}
}
CSV.foreach(file_path , headers: true) do |row|
puts row
index += 1
import_columns = Hash[row.map{|header, value| [header.to_s.downcase.strip, value.to_s]}]
row_atnd_type = import_columns["type de participant"].downcase.strip
unless import_columns["ville"].blank?
city_name = "#{import_columns["ville"]}"
city = City.where("name LIKE ?",city_name)
city = City.where("name LIKE ? and time_zone like ?",city_name, "%Europe%") if city.size > 1
import_columns["ville"] = city[0].id if city.present?
import_columns["ville"] = nil if city.blank?
end
options_array = []
#options_array << "J'accepte de recevoir les offres d'information de La Tribune" if import_columns["optin"] == "1"
#options_array << "J'accepte de recevoir les offres de nos partenaires" if import_columns["optin partenaires"] == "1"
options_array << "b" if import_columns["optin partenaires"] == "1"
options_array << "a" if import_columns["optin"] == "1"
attendee_type = super_hash["#{row_atnd_type}".to_sym][:type]
custom_fields = {
super_hash["#{row_atnd_type}".to_sym][:fields]["civilité"] => import_columns["civilité"].strip,
super_hash["#{row_atnd_type}".to_sym][:fields]["fonction"] => import_columns["fonction"].strip,
super_hash["#{row_atnd_type}".to_sym][:fields]["secteur d'activité"] => import_columns["secteur d'activité"].strip,
super_hash["#{row_atnd_type}".to_sym][:fields]["taille de société"] => import_columns["taille de société"].strip,
super_hash["#{row_atnd_type}".to_sym][:fields]["type de société"] => import_columns["type de société"].strip,
super_hash["#{row_atnd_type}".to_sym][:fields]["adresse"] => import_columns["adresse"].strip,
super_hash["#{row_atnd_type}".to_sym][:fields]["code postal"] => import_columns["code postal"].strip,
super_hash["#{row_atnd_type}".to_sym][:fields]["téléphone mobile"] => import_columns["téléphone mobile"].strip,
super_hash["#{row_atnd_type}".to_sym][:fields]["options d'adhésion"] => options_array
}
default_fields = {
attendee_type_id: attendee_type.id.to_s,
first_name: import_columns["prénom"].strip,
last_name: import_columns["nom"].strip,
email: import_columns["email"].strip,
company: import_columns["entreprise"].strip,
telephone: import_columns["telephone"].strip,
custom_fields: custom_fields
}
attendee = Attendee.create_draft(event.id, user)
default_fields = { attendee: default_fields, city_id: import_columns["ville"] }
puts default_fields
attendee.welcome_email_sent = true
if attendee.register(default_fields, user)
attendee.confirm_registration(user)
else
errors << "#{index + 1 }-#{import_columns["email"]}-#{attendee.errors.full_messages.join(',')}"
end
end
以上是关于ruby 参加者DB级别20的主要内容,如果未能解决你的问题,请参考以下文章
我在哪里可以编写要在部分中使用的方法? (Ruby on Rails)