Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DeepMerge changes ActiveRecord instance #572

Open
apotonick opened this issue Apr 6, 2023 · 2 comments
Open

DeepMerge changes ActiveRecord instance #572

apotonick opened this issue Apr 6, 2023 · 2 comments
Labels

Comments

@apotonick
Copy link

Hey folks,

I might be insane, but I'm experiencing something weird when using #deep_merge. In a tiny Rails 7 app, I have a Diagram ActiveRecord model.

diagram = Diagram.find(1)
puts diagram #=> #<Diagram id: 1, name: nil, ...>

class CtxHash < Hash
  include Hashie::Extensions::DeepMerge
end

puts CtxHash[{}].deep_merge(CtxHash[{diagram: diagram}])
#=> {diagram: #<Diagram id: nil, name: nil, ...>}

After the deep merge, the id of the ActiveRecord instance is nil. Any clues? I haven't looked into the source, yet, but remember hashie was doing some automatic subclassing somewhere when cloning.

@michaelherold
Copy link
Member

Hey Nick! #deep_merge performs a #deep_dup on non-Hashes. I believe ActiveRecord models are #duplicable? which means they have #dup called on them. That exhibits the behavior that you're seeing.

Hashie doesn't support rich objects well and never has. It's more of a data munging library that concerns itself with primitives.

Introducing a workaround for ActiveRecord is tricky because we can't depend on Rails constants in the library, but I would be open to a patch that doesn't break backwards compatibility.

@apotonick
Copy link
Author

Hey Michael! Thanks for responding so quickly! 💚 My first question here would be (regarding hashie as a beautiful munger for primitive datatypes!!!), why are you calling dup on a "flat" object in the first place?

@dblock dblock added the bug? label Aug 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants