Skip to content

Different handling of ActiveRecord_Relation since 3.16.17 #1020

Description

@gabsi20

Hi, we noticed a breaking change starting with oj 3.16.17 and still present in 3.17.3.

In our Rails app we use:

Oj.optimize_rails
Oj.default_options = { mode: :compat }

Serializing an ActiveRecord::Relation with to_json options now behaves differently:

vendors.to_json(only: [:name])

Before 3.16.17, this returned:

=> "[{\"vendor\":{\"name\":\"vendor-2\"}},{\"vendor\":{\"name\":\"vendor-3\"}},{\"vendor\":{\"name\":\"vendor-1\"}},{\"vendor\":{\"name\":\"vendor-4\"}},{\"vendor\":{\"name\":\"vendor-0\"}}]"

Since 3.16.17, it returns:

=> "[{},{},{},{},{}]"

In our case this results in empty hashes / missing model data, which breaks existing Rails code relying on ActiveRecord::Relation#to_json(options).
Is this intentional? If yes, is there a compatibility setting to restore the old behavior while keeping Oj.optimize_rails enabled?

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