4

I have Locations models - countries/regions/cities which have in total 50k plus records. i have added the configuration to these models.

Is there a way through the command line i can create the slugs for these models at one go instead of editing and saving all the models.

strivedi183
  • 4,749
  • 2
  • 31
  • 38
Harsha M V
  • 54,075
  • 125
  • 354
  • 529

2 Answers2

7

Save

To help you further - @iceman is right - you need to loop through your slugged models & save them again. friendly_id recommends this by doing it in the rails console:

$ rails c
$ Location.find_each(&:save)

This should help Rails load each of the items, and then save them immediately. This will trigger the slug generation functionality of friendly_id, populating the slug columns of your Location records

Richard Peck
  • 76,116
  • 9
  • 93
  • 147
4

friendly_id updates the slug on save-ing. I did it like this for my project, but that only included about 5k items, so this works but could take some time depending on your setup.

Model.all.map(&:save)
Eyeslandic
  • 14,553
  • 13
  • 41
  • 54
  • 2
    @HarshaMV Thru the command line as you were talking about :) `rails c` – Eyeslandic Jun 29 '14 at 22:19
  • hehe :D am confused. whats the first line and the rest of the block. do i need to run both? – Harsha M V Jun 30 '14 at 02:17
  • i tried doing that. also added this in the model http://pastebin.com/HRpfijAS but still it doesnt update – Harsha M V Jun 30 '14 at 02:37
  • 1
    @HarshaMV Try `Model.first.save!`, with `!` you'll get an exception, maybe it will bring you closer to a solution. There must be something missing in your setup. – Eyeslandic Jun 30 '14 at 11:27
  • for an "Image" model, it does not do anything on my side. the slug never updates on an existing record – Ben Feb 01 '15 at 20:47