I am using Mongoid::Slug https://github.com/digitalplaywright/mongoid-slug.
I am trying to update a set of data, which in part has been created prior to the usage os Mongoid::Slug. So I have a bunch of objects with _slugs = [] and slug.nil?. I want to update these in a rake task, but somehow I am failing miserably.
All objects derive their slugs from the 'title' attribute.
slug :title, history: true
The rake task:
class SlugGenerator
def run
all_objects = get_array_of_all_objects
objects_without_slugs = all_objects.select { |obj| obj.slug.blank? }
pp "#{objects_without_slugs.count} without Slug."
objects_without_slugs.each do |obj|
p "item #{obj.title} has no slug"
p obj.save
end
end
def get_array_of_all_objects
all_objects = []
all_portfolios = Portfolio.all
all_objects << all_portfolios
all_items = all_portfolios.map { |p| p.portfolio_items }.flatten #array embedded in portfolio
all_objects << all_items
all_images = all_items.map { |item| item.images }.flatten #array embedded in pf_item
all_objects << all_images
all_objects.flatten
end
end
Save returns true. Still, these object continue without slugs. Even if I change the title prior to saving, I get no different result.
What am I missing