Skip to content

Rails 8.1 misbehave using Mobility with ActiveModel normalizes #682

@pontflorian

Description

@pontflorian

Environment

  • Mobility version: 1.3.2
  • Mobility backend: jsonb
  • Rails version: 8.1.1
  • Ruby version: 3.4.6
  • Database: PostgreSQL

Description

It seems there is an issue using normalizes with Mobility in ActiveRecord since Rails 8.1.

# config/initializers/mobility.rb

Mobility.configure do
  plugins do
    backend :jsonb
    active_record
    reader
    writer
    backend_reader
    query
    cache
    column_fallback true
    fallbacks true
    presence
  end
end
# app/models/content.rb

class Content < ApplicationRecord
  extend Mobility

  normalizes :data, with: -> { it.compact_blank }

  translates :data, writer: false, column_fallback: false
end

Expected behavior

c = Content.new(data: { fr: "Non", en: "No", es: nil }) #  => <Content id: nil, data: {"fr" => "ok", "en" => "nope"}>
c.save # => true

Actual behavior

c = Content.new(data: { fr: "Non", en: "No", es: nil }) #  => <Content id: nil, data: {"fr" => "ok", "en" => "nope"}>
c.save # => undefined method 'compact_blank' for an instance of String (NoMethodError)

Error

normalizes method tries to execute a proc on a string (translated attribute) instead of the original column (Hash).
The same code works on Rails 8.0.

Do not hesitate to ask if you need complementary information.

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