rigelstpierre
rigelstpierre

Reputation: 544

Friendly_id not creating slugs for old records

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.

Upvotes: 7

Views: 2866

Answers (1)

Ruecktenwald
Ruecktenwald

Reputation: 141

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

Upvotes: 2

Related Questions