简体   繁体   中英

Rails single-table inheritance

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM