Skip to content

Normalizes attribute does not work as expected #84

@kmdtmyk

Description

@kmdtmyk

This model uses ActiveRecord::Base.normalizes feature it's from rails version 7.1.

https://api.rubyonrails.org/classes/ActiveRecord/Normalization/ClassMethods.html#method-i-normalizes

class Book < ApplicationRecord
  # Convert full-width characters to half-width
  normalizes :title, with: ->(value){
    value.tr('A-Za-z', 'A-Za-z')
  }

  search_scope :search do
    attributes :title
  end
end
Book.create(title: 'ABCDE') # full-width
=> #<Book id: 1, title: "ABCDE"> # converted to half-width
Book.search('ABC').to_sql
=> "SELECT * FROM books WHERE title ILIKE '%ABC%'" # expected
=> "SELECT * FROM books WHERE title ILIKE '%ABC%'" # actual

I tried to create a pull request. But it was too difficult for me. 😢

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions