I just added the Friendly_id gem to my rails project looking to not use the database id and create a slug, but I can't seem to create slugs for old records. My model looks like this.
class Mapping < ActiveRecord::Base
extend FriendlyId
friendly_id :title, use: :slugged
# Friendly_Id code to only update the url for new records
def should_generate_new_friendly_id?
new_record? || slug.blank?
end
end
I then am running Model.find_each(&:save)
but it keeps spitting out a nil result. I've tried commenting out the should_generate_new_friendly_id
completely but with no luck. Anyone see what I'm doing wrong here?
EDIT
I rolled my database back and rewrote my migratations and that has appeared to have fixed the issue.
If anyone finds this 6 year old question:
Without a "do and end" block:
find_each returns an Enumerable (not an ActiveRecord Class)
Use:
Model.find_each.select(&:save)
Notice the added "select" method
Doing stuff with Rails’ find_each
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With