I have a category_aliases table, which has aliase column (array type), and category_id column, which points to categories table.
I have products table, which has category column.
I have categories table.
I want to loop through all products (a lot), which has old categories, and map these to new categories.
So I need to check if any of category_aliases.aliase (aliase is an array of old categories) includes this product's category, and if yes, I want to map it to new category from categories table.
What I tried is:
Product.all.each do |p|
CategoryAlias.all.each do |ca|
if ca.aliase.include? p.category
p.update_column(:category, Category.find(ca.category_id).name)
else
p.update_column(:category, 'undefined')
end
end
end
But I am surely missing something, because even though I know, that there should almost always be a match, it updates p.category to 'undefined' all the time.
puts/logger driven development; add a lot ofputsorlogger.infostatements at each step, and pinpoint exactly where the expected output is different from actual output. :-)aliasean array inside the database or is it done usingserialize(i.e. a blob of YAML inside the database)?t.text :aliase, aray: true)