All threads / Nested preference settings with activerecord store

Ask A Question

Notifications

You’re not receiving notifications from this thread.

Nested preference settings with activerecord store

Alan Reid asked in Rails

As per this Episode 191 is there any way I can add nested settings? for example:

{
"wants_reminders": true,
"wants_deals_offers": true,
"subscribed_to_newsletter": true,
"something_here": {
    "option1": "data",
    "option2": "data"
}

There's nothing builtin to Rails to read or write option1 or option2 directly.

You can assign a hash to something_here and it will work just fine though so you can write your own helper method to read from that, just have to assign the hash though when you want to change it.

Thanks for the reply mate, so basically i want to store

{
  "energy_provider": {
      "name": "data",
      "renewal": "data"
  },
  "insurance_provider": {
      "name": "data",
      "renewal": "data"
  }
}

Do you think it would be best to have JSONB fields in the table to store these then? Or do you know of a more efficient way you could suggest please?

I found this article, which seems to be interesting. And maybe worth a look at.

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 38,558+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.