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"
}
Reply

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.

Reply

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.

Reply
Join the discussion
Create an account Log in

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

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

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