Fix duplicate record with embeds_many.

This commit is contained in:
BoHung Chiu 2021-08-31 18:58:02 +08:00
parent 12b9e91751
commit f086976912
1 changed files with 16 additions and 0 deletions

View File

@ -473,6 +473,22 @@ class SeminarMain
no_dup_flag = new_object.send(f).present?
elsif clone_target.relations[f].macro == :has_many
no_dup_flag = new_object.send(f).to_a.count != 0
elsif clone_target.relations[f].macro == :embeds_many #Fix localize fields
if new_object.send(f).to_a.count != 0
need_fix_fields = new_object.send(f).to_a[0].fields.select{|k,v| (v.options[:localize] rescue false)}.keys
locale = I18n.locale.to_s
embeded_records = new_object.send(f).map do |embeded_record|
need_fix_fields.each do |f|
if (embeded_record[f][locale].class != String rescue false)
embeded_record.send("#{f}_translations=",embeded_record[f][locale])
else
embeded_record.send("#{f}_translations=",embeded_record[f])
end
end
embeded_record
end
new_object.send("#{f}=",embeded_records)
end
end
if clone_target.relations[f].macro == :belongs_to || clone_target.relations[f].class_name == "MemberProfile"
if @records_all["#{f}_ids"].nil?