Search code examples
ruby-on-railsruby-on-rails-4friendly-id

Friendly ID not working as of Rails 4.2?


I recently upgraded to rails 4.2 and I found that friendly ID stopped working. Not sure if this a bug or if I am literally just failing at using friendly id.

After the update my tests started failing, for example I have the following test:

  context "Fiendly ID" do
    it "should find by name" do
      permission = FactoryGirl.create(:can_read)
      Xaaron::Permission.find(permission.permission_name.parameterize).should_not eql nil
    end
  end

This test never use to fail but now its spitting out:

 Failure/Error: Xaaron::Permission.find(permission.permission_name.parameterize).should_not eql nil
 ActiveRecord::RecordNotFound:
   Couldn't find Xaaron::Permission with 'id'=can_read2
 # ./.bundle/gems/gems/activerecord-4.2.0/lib/active_record/core.rb:154:in `find'
 # ./spec/models/xaaron/permission_spec.rb:21:in `block (3 levels) in <top (required)>'

With that in mind here is my model:

module Xaaron
  class Permission < ActiveRecord::Base
    extend FriendlyId
    friendly_id :permission_name, use: [:slugged, :finders, :history]

    has_many :roles_permissions
    has_many :roles, :through => :roles_permissions

    validates :permission_name, presence: true, uniqueness: true

    def should_generate_new_friendly_id?
      permission_name_changed?
    end
  end
end

notice the :finders. I am running 5.0.3 for Friendly ID. Is this something new with active record or have I failed at using Friendly ID?


Solution

  • The finders module is compatible with Rails 4.2. only in the 5.1. version (not yet released). You can of course already test the version but keep in mind that it's still in beta.

    gem "friendly_id", "5.1.0.beta.1"