0

I have a class TrafficRule that is defined like this:

TrafficRule
- name:string
- type:string
- details:text

The details parameter will be a JSON object that will store a set of details for a given traffic rule, and the type parameter will define what that JSON object looks like. So for one rule type the object might be an array, and for another the object might be a hash. I would like the TrafficRule STI class to define what the details object looks like.

Has anyone come across a design pattern that solves this use case well? Maybe I need to change the way my objects are associated?

Ideally, I would love to be able to edit this object in ActiveAdmin and have the form actually customize for the type of input that it's expecting.

kid_drew
  • 3,857
  • 6
  • 28
  • 38
  • If you're using JSON in your schema, might be worth checking out [Postgres](http://postgresql.org/) which has a very powerful native JSON datatype. Rails supports this in your schema as `:json` without needing to add a `serialize` declaration. – tadman Jul 24 '14 at 18:36
  • I'm using Postgres, but I wasn't aware of the json data type. Thanks for the heads up. – kid_drew Jul 25 '14 at 20:06

1 Answers1

0

Having schemaless data and STI might be a little confused from a design perspective, one of those should be sufficient without needing the other, but it can still work.

You can always trigger the proper view for this using something like:

render(partial: "#{model.name.underscore}_edit")

Where you'd have partials named something like _example_edit and _another_example_edit for the Example and AnotherExample classes respectively.

tadman
  • 208,517
  • 23
  • 234
  • 262
  • Do you have a recommendation for how to structure the design better? I've been scratching my head about it for a while now. – kid_drew Jul 25 '14 at 20:09
  • Not really enough information in your question to make a call here. I'd try the simplest thing that could possibly work, and if that's insufficient, incrementally add to it until it does. – tadman Jul 28 '14 at 16:32