Search code examples
postgresqlruby-on-rails-5cancancan

Case insensitive search with CanCanCan?


I'm trying to do a case insensitive search with CanCanCan. I'm using a custom variable called code as follows:

class PromotionsController < ApplicationController
  load_and_authorize_resource find_by: :code, id_param: :code

  # GET /api/v1/promotions/:code
  def show; end
end

Is there a was to make the look up that auto populates @promotion to be case insensitive while still using CanCanCan's query?

Or is this something that needs to be accomplished at a database level?


Solution

  • I'd use citext, a case insensitive text type in PostgreSQL.

    This lives in an extension so you'll have create that first:

    CREATE EXTENSION citext;
    ALTER TABLE promotions ALTER code TYPE citext;