I'm having some trouble coming up with the most efficient data model for storing logical criteria. The criteria can be as follows:
- Field = Value
- Field != Value
- Field1 = Value1 AND Field2 = Value2
- Field1 = Value1 OR Field2 = Value2
But can also be nested as so:
- (Field1 = Value1 OR Field2 = Value2) AND Field3 = Value3
The nesting can be infinitely deep.
I started with a model like this:
Criterion
Field Name
Comparison Operator
Comparison Value
This allows me to express simple criteria such as 'Field = Value'.
How should I go about chaining the Criterion
together using AND/OR statements and what is the best way to handle the nesting?
FYI (might add more context) - this is for a Rails/Mongoid project. I have a Document
which embeds_many :criteria
. I need to be able to call Document.criteria_string
and have the document iterate over all the criteria and assemple it into a single statement accounting for the nesting, chaining and positioning of the criteria.
Thanks!