activerecord-typedstore icon indicating copy to clipboard operation
activerecord-typedstore copied to clipboard

Is there a way to master a nested json with a json column?

Open a0x opened this issue 6 years ago • 1 comments

For example:

# 'settings' is a json-typed column for the model User in database(pg or mysql)
class User < ApplicationRecord
  typed_store :settings do |s|
    s.boolean :new_msg, default: true, null: false
    s.boolean :some_settings, default: false, null: false
    s.array :other_settings, default: [], null: false
  end
end

And I want to add some nested json in attributes in the setting column, just like:

{
  "new_msg": true,
  "some_settings": false,
  "other_settings": [
    "a",
    "b",
    "c"
  ],
  "nested_settings": {
    "setting_a": true,
    "setting_b": false
  }
}

Is there any way to make it happen with TypedStore?

a0x avatar Jul 15 '19 07:07 a0x

loses typing, but the any type will allow you to store nested data if you need to

 typed_store :settings do |s|
    s.boolean :new_msg, default: true, null: false
    s.boolean :some_settings, default: false, null: false
    s.string :other_settings, array: true, default: [], null: false
    s.any :nested_setting, null: false
  end

schpet avatar Nov 29 '23 21:11 schpet