1 year ago

#373469

test-img

Peter Gerdes

Rails: Use custom class for activerecord attribute (update in place)

If I have a jsonb column activerecord returns the attribute as a hash but somehow retains a reference to that object so it is able to detect changes. That is, code behaves like the following

pub = Publication.find("554a1791-e690-4a45-8207-db6b66fa3480")
md = pub.metadata #jsonb column
pub.metadata_changed?  #false
md['foo'] = 'bar'
pub.metadata_changed?  #true

I'd like to replace the hash class with an instance of a custom class but retain the modification in place behavior. What do I need to do to achieve this? Do I overload some kind of method to check if the object is dirty?

ruby-on-rails

activerecord

rails-activerecord

0 Answers

Your Answer

Accepted video resources