madeintandem / jsonb_accessor

Adds typed jsonb backed fields to your ActiveRecord models.
MIT License
1.11k stars 93 forks source link

Update json_accessor attributes after becomes! #150

Closed rusikf closed 2 years ago

rusikf commented 2 years ago

Hi @haffla and @caiohsramos, thank's for your gem! Please advise about this issue:

If we have STI ActiveRecord models and run 'becomes!' to change the STI type:

class Child1 < Parent
 jsonb_accessor :data, email: :string,
end
class Child2 < Parent
 jsonb_accessor :data, name: :string,
end

instance = Child1.becomes!(Child2);
instance.update!(name: 'a')

We receive the error "NoMethodError: undefined method `name'"

Question: Is it possible to reload attributes for the instance ( without reload! because of DB transaction ) ??

I tried instance.class.jsonb_accessor :data, name: :string; instance.update!(...), but instance doesn't refreshed after changes in the class.

Thanks! :man_mechanic:

caiohsramos commented 2 years ago

@rusikf I think this is actually a Rails issue. Even if you define name and email as simple ActiveModel attributes the assignment will fail

Edit: https://github.com/rails/rails/issues/45702

rusikf commented 2 years ago

@caiohsramos Thank's a lot! As I read the latest update of this rails issue, we cannot use becomes to change STI to sibling class. => In our example, we don't have a beautiful way to change the type and set jsonb attributes. :thinking:

haffla commented 2 years ago

Closing because it seems that this isn't a jsonb_accessor issue.