Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Friendly_id create slugs for a existing model

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.

like image 876
Harsha M V Avatar asked Dec 20 '22 13:12

Harsha M V


2 Answers

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

like image 164
Richard Peck Avatar answered Dec 30 '22 05:12

Richard Peck


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)
like image 41
Eyeslandic Avatar answered Dec 30 '22 06:12

Eyeslandic