Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 204
rigelstpierre Avatar asked Oct 09 '12 20:10

rigelstpierre


1 Answers

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

like image 131
Ruecktenwald Avatar answered Oct 21 '22 07:10

Ruecktenwald