ruby-on-railsdatabaseschemamodeling

how to design db schema for if-else-then condition


first of all, I am sorry in advance for my bad english. It would be difficult to understand what I'm trying to say, but I will try my best to explain what i want to ask.

**my question is how to design if-else-then condition db architecture. below is details. **

I want to make kinda routing system based on user's answer. when I send inquiry form to user, and user answer the questions, then my system send the user to a link based on user's answer.

For example, there are two questions,

  1. what's your age range?
  1. what's your name?

and

if question1's answer is a or question1's answer is b then go to A Link

if question1's answer is c and question2' answer contains 'kevin' then go to B Link

I don't have any idea how to design db for those conditions.

I Have 2 models. but it is just temporary modeling.

routes

id | destination_type | destination_url  | rules            |
-------------------------------------------------------------
1  | external_url     | https://a.com    | Idon't have idea |

// if user's answer matches with rules, then send user to destination_url
question

id | kind   | label                 | options                |
--------------------------------------------------------------
1  | radio  | what's your age range | [{value: '20-30'},...] |
2  | name   | name                  | {}                     |

I want to design db table for rules but it would be ok if there is another idea other than designing db.


Solution

  • I think putting redirection links inside the database is breaking the MVC logic applied in rails.

    Your condition formulated inside the text sounds like controller logic to me, e.g.

    AnswerController
      def a
        if things
          redirect_to a_path
        else
          redirect_to b_path
        end
      end
    end
    

    But if you really try to make this fully dynamic a.k.a. full logic in database you can think about putting the medhod calls in the datebase as attribute Question.my_string_on_answer_a and work with my_question.send(my_question.my_string_on_answer_a) or redirect_to "my_string_on_answer_a" inside a Controller method or something.

    Inn the end it sounds you like to put the routing targets in the database. Which is quite akward.

    Sending something to my_object in ruby is like invoking/calling the method.

    I used this lately to dynamically call methods. I had rows of a form implemented in a model. Tho "rows" could be called by object.send("r123") or object.send("r54")

    calling methods accordingly e.g object.r123 or object.r54

    Your question is quite abstract. Sorry if my answer is too abstract for you to directly implement but at least should be food for thought! Happy to discuss further.

    Please also think about that there are also boolean attributes in the database. Maybe this make things easier but 100% not sure.