0
votes

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.

1
If you're using JSON in your schema, might be worth checking out Postgres which has a very powerful native JSON datatype. Rails supports this in your schema as :json without needing to add a serialize declaration.tadman
I'm using Postgres, but I wasn't aware of the json data type. Thanks for the heads up.kid_drew

1 Answers

0
votes

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.