From f086976912ffb3bb7d72abd2bd5fbf68321627a3 Mon Sep 17 00:00:00 2001 From: Bohung Date: Tue, 31 Aug 2021 18:58:02 +0800 Subject: [PATCH] Fix duplicate record with embeds_many. --- app/models/seminar_main.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/app/models/seminar_main.rb b/app/models/seminar_main.rb index 114ee0a..ce56eba 100644 --- a/app/models/seminar_main.rb +++ b/app/models/seminar_main.rb @@ -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?