[Feature] ActiveRecord support Models and Associations for entities that reside in a JSONB column

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[Feature] ActiveRecord support Models and Associations for entities that reside in a JSONB column

Josh Marchello-2
I'm considering starting a new PR to update ActiveRecord and wanted to check here first to see if it is a viable feature. In Short I would like to be able to define models for JSONB objects and have them supported as first class associations just like foreign-key style associations.

This would provide the ability for developers to store objects in JSONB while still enjoying the benefits of ActiveRecord such as association validation.

I am currently attempting to accomplish this using ActiveModel::Model and the Attributes API, but find a few things are more difficult to support such as validating my JSONB objects when the "Parent" (the model they 'belong_to') is saved.
Tentatively I'm thinking of something like `has_many :posts, json: true` or something like that, though of course I'm open to feedback in any regard.

Would this be something I should look into or are there technical/philosophical reasons why this would not work?  Thanks in advance.

- Josh Marchello

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-core/d607960a-5a7f-4354-9d9a-eff12dec39cb%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: [Feature] ActiveRecord support Models and Associations for entities that reside in a JSONB column

Dmitry Tsepelev
Hi Josh!

There is a number of gems that can do this (or similar) thing for you:

- https://github.com/DmitryTsepelev/store_model
- https://github.com/palkan/store_attribute
- https://github.com/marshall-lee/nested_record

On Wednesday, 2 October 2019 05:23:14 UTC+3, Josh Marchello wrote:
I'm considering starting a new PR to update ActiveRecord and wanted to check here first to see if it is a viable feature. In Short I would like to be able to define models for JSONB objects and have them supported as first class associations just like foreign-key style associations.

This would provide the ability for developers to store objects in JSONB while still enjoying the benefits of ActiveRecord such as association validation.

I am currently attempting to accomplish this using ActiveModel::Model and the Attributes API, but find a few things are more difficult to support such as validating my JSONB objects when the "Parent" (the model they 'belong_to') is saved.
Tentatively I'm thinking of something like `has_many :posts, json: true` or something like that, though of course I'm open to feedback in any regard.

Would this be something I should look into or are there technical/philosophical reasons why this would not work?  Thanks in advance.

- Josh Marchello

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-core/331b0e32-50ce-4b6f-a7f5-28e923e56334%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: [Feature] ActiveRecord support Models and Associations for entities that reside in a JSONB column

Josh Marchello-2
Thanks Dmitry, your store_model gem looks particularly well done. I'll give it a try!

On Wednesday, October 2, 2019 at 12:31:04 AM UTC-5, Dmitry Tsepelev wrote:
Hi Josh!

There is a number of gems that can do this (or similar) thing for you:

- <a href="https://github.com/DmitryTsepelev/store_model" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2FDmitryTsepelev%2Fstore_model\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFY33Mj7kGH609a7VHp9mDF-nQRDw&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2FDmitryTsepelev%2Fstore_model\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFY33Mj7kGH609a7VHp9mDF-nQRDw&#39;;return true;">https://github.com/DmitryTsepelev/store_model
- <a href="https://github.com/palkan/store_attribute" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Fpalkan%2Fstore_attribute\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHHnharOh87AhaPS77oLQq0wcqV7A&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Fpalkan%2Fstore_attribute\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHHnharOh87AhaPS77oLQq0wcqV7A&#39;;return true;">https://github.com/palkan/store_attribute
- <a href="https://github.com/marshall-lee/nested_record" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Fmarshall-lee%2Fnested_record\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEWA6jBT06OlLs_rz_ADwyS1PVj3g&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Fmarshall-lee%2Fnested_record\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEWA6jBT06OlLs_rz_ADwyS1PVj3g&#39;;return true;">https://github.com/marshall-lee/nested_record

On Wednesday, 2 October 2019 05:23:14 UTC+3, Josh Marchello wrote:
I'm considering starting a new PR to update ActiveRecord and wanted to check here first to see if it is a viable feature. In Short I would like to be able to define models for JSONB objects and have them supported as first class associations just like foreign-key style associations.

This would provide the ability for developers to store objects in JSONB while still enjoying the benefits of ActiveRecord such as association validation.

I am currently attempting to accomplish this using ActiveModel::Model and the Attributes API, but find a few things are more difficult to support such as validating my JSONB objects when the "Parent" (the model they 'belong_to') is saved.
Tentatively I'm thinking of something like `has_many :posts, json: true` or something like that, though of course I'm open to feedback in any regard.

Would this be something I should look into or are there technical/philosophical reasons why this would not work?  Thanks in advance.

- Josh Marchello

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-core/930f1f4e-ed61-47a7-b1e3-1484a1dfbd7e%40googlegroups.com.