Friendly_id not working version 5.3 rails 6

I’ve been working on an app and I installed friendly_id version 5.3 and everything was working.

However, recently working on the app I’ve found friendly_id not working at all on anything it’s been set on. So for this, I’m going to use the articles.

I can go to the article using http://localhost:3000/articles/1 and not http://localhost:3000/articles/another-test

This is the error enter image description here

and here is the code, as stated before everything was working so I’m not sure why all of a sudden everything is broke.

article.rb

class Article < ApplicationRecord
  extend FriendlyId
  friendly_id :title, use: :slugged

  has_one_attached :article_image
  has_rich_text :content
  has_many :article_users
  has_many :authors, through: :article_users, source: :user

  validates :title, presence: true
  validates :authors, presence: true
  validates :content, presence: true
  validates :article_image, presence: true

  def name_of_authors
    authors.map(&:name).to_sentence
  end

  def about_authors
    authors.map(&:about).to_sentence
  end
end

ApplicationController (incase it’s relevant)

class ApplicationController < ActionController::Base
  before_action :configure_permitted_parameters, if: :devise_controller?
  before_action :authenticate_user!

  protect_from_forgery with: :exception
  rescue_from CanCan::AccessDenied do |exception|
    redirect_to main_app.root_url, alert: exception.message
  end

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [:name])
    devise_parameter_sanitizer.permit(:account_update, keys: [:name, :about, :avatar])
  end
end

ArticlesController

class ArticlesController < ApplicationController
  load_and_authorize_resource
  before_action :set_article, only: [:show, :edit, :update, :destroy]
  skip_before_action :authenticate_user!, only: [:index, :show]

  .....

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_article
      @article = Article.friendly.find(params[:id])
    end

    # Only allow a list of trusted parameters through.
    def article_params
      params.require(:article).permit(:title, :content, :article_image, :author_ids)
    end
end

schema

create_table "articles", force: :cascade do |t|
  t.string "title"
  t.datetime "created_at", precision: 6, null: false
  t.datetime "updated_at", precision: 6, null: false
  t.string "slug"
  t.index ["slug"], name: "index_articles_on_slug", unique: true
end

create_table "friendly_id_slugs", force: :cascade do |t|
    t.string "slug", null: false
    t.integer "sluggable_id", null: false
    t.string "sluggable_type", limit: 50
    t.string "scope"
    t.datetime "created_at"
    t.index ["slug", "sluggable_type", "scope"], name: "index_friendly_id_slugs_on_slug_and_sluggable_type_and_scope", unique: true
    t.index ["slug", "sluggable_type"], name: "index_friendly_id_slugs_on_slug_and_sluggable_type"
    t.index ["sluggable_type", "sluggable_id"], name: "index_friendly_id_slugs_on_sluggable_type_and_sluggable_id"
end

Any help would be great as I’m really confused here.

Yes, I have runArticle.find_each(&:save) in the console.

irb(main):001:0> Article.find_each(&:save)
  Article Load (1.8ms)  SELECT "articles".* FROM "articles" ORDER BY "articles"."id" ASC LIMIT $1  [["LIMIT", 1000]]
   (0.3ms)  BEGIN
  User Load (3.6ms)  SELECT "users".* FROM "users" INNER JOIN "article_users" ON "users"."id" = "article_users"."user_id" WHERE "article_users"."article_id" = $1  [["article_id", 1]]
  ActionText::RichText Load (1.8ms)  SELECT "action_text_rich_texts".* FROM "action_text_rich_texts" WHERE "action_text_rich_texts"."record_id" = $1 AND "action_text_rich_texts"."record_type" = $2 AND "action_text_rich_texts"."name" = $3 LIMIT $4  [["record_id", 1], ["record_type", "Article"], ["name", "content"], ["LIMIT", 1]]
  ActiveStorage::Attachment Load (2.5ms)  SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3 LIMIT $4  [["record_id", 1], ["record_type", "Article"], ["name", "article_image"], ["LIMIT", 1]]
   (0.2ms)  COMMIT
=> nil

Answer

There is a conflict between the cancancan gem. You need to add a file called cancan.rb in config/initializers with the following

require_dependency 'cancan/model_adapters/active_record_4_adapter'

if defined?(CanCan)
  class Object
    def metaclass
      class << self; self; end
    end
  end

  module CanCan
    module ModelAdapters
      class ActiveRecord4Adapter < ActiveRecordAdapter
        @@friendly_support = {}

        def self.find(model_class, id)
          klass =
              model_class.metaclass.ancestors.include?(ActiveRecord::Associations::CollectionProxy) ?
                  model_class.klass : model_class
          @@friendly_support[klass]||=klass.metaclass.ancestors.include?(FriendlyId)
          @@friendly_support[klass] == true ? model_class.friendly.find(id) : model_class.find(id)
        end
      end
    end
  end
end