Skip to content
Snippets Groups Projects
Commit 40064756 authored by Taico Aerts's avatar Taico Aerts
Browse files

In seeding, actually link to the corresponding objects

Previously, we would pass ids to the creation handler. However, this
does not pass validations whenever the association is non-optional (i.e.
belongs_to). With these changes, we pass the actual Rails objects
instead of just the ids, which does pass the validations and sets it
properly.
parent 141a6d8f
Branches
Tags
1 merge request!24Release 22-09-2022
...@@ -63,6 +63,26 @@ sorted.each do |f, type| ...@@ -63,6 +63,26 @@ sorted.each do |f, type|
# otherwise, no need to worry, just create it # otherwise, no need to worry, just create it
[seed] [seed]
end end
# Transform all _id relations into the actual rails objects to pass validations
seeds = seeds.map do |seed|
columns = type.column_names.select { |name| name.match(/^.*_id$/) }
new_seed = seed.deep_symbolize_keys
columns.each do |column|
begin
column_type_name = column.chomp('_id')
column_type = column_type_name.classify.constantize
new_seed = new_seed.except(column.to_sym)
.merge(column_type_name.to_sym => column_type.unscoped.find(seed[column.to_sym]))
rescue StandardError
# Either the type does not exist or the value specified as the id is not valid, ignore.
next
end
end
new_seed
end
# Actually create the objects and count successes
objs = type.create seeds objs = type.create seeds
skipped += objs.select { |o| o.errors.any? }.size skipped += objs.select { |o| o.errors.any? }.size
created += objs.select { |o| !o.errors.any? }.size created += objs.select { |o| !o.errors.any? }.size
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment