Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: Friendly_id does not generate slugs for old records

I've used the FriendlyId gem (version 4.0.8) with a Rails application. I've followed the tutorial on RailsCasts and based on the documentation, I have to run Model.find_each(&:save) on rails console to generate slugs for old records. However, when I do this, all of my old records still have nil for their slug attributes, so it doesn't really change the url's.

Am I doing something wrong? This only happens on production by the way. It works fine on development.

Update:

My model looks like this:

class Member < ActiveRecord::Base
  extend FriendlyId
  friendly_id :name, use: :slugged

  belongs_to :gym

  attr_accessible :category, :name, :description
  validates :category, :name, :description, :presence => true

  has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }

  def self.search(search)
    if search.present?
      where("name LIKE ?", "%#{search}%")
    else
      find(:all)
    end
  end

  def should_generate_new_friendly_id?
    new_record?
  end
end
like image 998
gerky Avatar asked Aug 19 '12 12:08

gerky


1 Answers

should_generate_new_friendly_id? is returning false since new_record? is false, since your records already exist.

Delete the should_generate_new_friendly_id? method, or try this & re-run:

def should_generate_new_friendly_id?
  new_record? || slug.blank?
end

See also: Rails Friendly_Id on Heroku, Heroku not updating slugs

like image 117
tee Avatar answered Nov 05 '22 14:11

tee