2

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

Jan
  • 6,532
  • 9
  • 37
  • 48
  • Call the 'build_slug()' method on the objects without a slug. – Andreas Sæbjørnsen Sep 14 '14 at 16:15
  • @AndreasSæbjørnsen, this does not work out of some reason. So far the only way to fix this, was to clone the object, delete the original and reattach the clone. I lose IDs that way, so I am not happy with that, but it is the best I was able to come up with. – Jan Sep 15 '14 at 14:15
  • That should not be necessary and is a bit unfortunate. Have you validated that the documents are saving properly? Do you have some validation that is stopping the save with an error? – Andreas Sæbjørnsen Sep 17 '14 at 22:24
  • They validate and in pry, I can save them. So it seems. Once I reload the data, nothing has been saved. It is really weird, might even be a mongoid or mongodb bug... – Jan Oct 09 '14 at 20:31
  • Do you see any errors in the mongodb console? I suggest setting the slug on a single document on the rails conmmandline and saving it. After you save run valid? method on model to make sure it was saved properly and then also inspect the mongodb logs. – Andreas Sæbjørnsen Oct 16 '14 at 21:18
  • I think it is, in reality, a mongoid bug. I do not see errors, it is just, whatever I do, it does not become persisted. But I have not yet inspected the mdb logs. I might find something there... – Jan Oct 17 '14 at 16:04

2 Answers2

1

I got the same issue and resolved it by adding

set_callback :update, :before, :build_slug
set_callback :save,   :before, :build_slug

to my model.

After that, I just ran over all my entities in the rails console, updated the 'updated_at' field to DateTime.now saved it and voila! the slugs have been created.

Thomas
  • 71
  • 1
  • 4
0

Suppose you have model Portfolio and you need to add slug for all legacy objects then you just need to run this in console or you can go for new rake task:

Portfolio.each do |portfolio|
   portfolio.build_slug
   portfolio.save
end
Anuja Joshi
  • 698
  • 6
  • 13