Adding slugs to existing records in Rails!...

 So if you read PART 1 of this guide you’ve got a good start on getting the slugs up and running... But for the rest of us, accessing the site will start throwing up some nasty errors. That’s because we have all these posts that don’t have a slug! So trying to see any of these posts is going to throw up errors because there’s nothing there! 

 That’s why I had that last method in the model; 
 def self.create_slugs
Post.all.each do |post|
post.set_slug
end
 end
 We are going to call this from the rails console, and it’s going to give all of our posts a slug!
 **BUT!!
Because we have these two lines at the top; the before_create and the before_update (mostly the update one…) we will get a stack error because when we update a post it calls set_slug which calls the update which calls set_slug…. And on and on and on… So comment those out for a sec…
class Post < ApplicationRecord
 #before_update :set_slug
 #before_create :set_slug
 And then go to your terminal in your base directory and enter the rails console;
home@yourcomputer:~/RubymineProjects/project$ rails console 
 and you’ll get a new prompt;
Running via Spring preloader in process 5754
Loading development environment (Rails 5.2.1)
irb(main):001:0> 
 Now I had a small issue here with the Spring Preloader and it kept giving me an error like “No record found” And I had to kill the Springboot preloader or reset it or something… If you hit this issue there’s an answer on SO that was super simple and solved it completely. Sorry I didn’t keep track of it...
 In the rails console enter the following;
irb(main):001:0> Post.create_slugs
 And it should loop through all your posts and give em all a slug! Yay! Now just un-comment the before and create lines at the top and launch your app! You’ll be all good!

Comments (Coming Soon)