aleherzko
aleherzko

Reputation: 90

How to save Responses of dynamically created Forms by Users

I'm relatively new to Ruby on Rails.

I have an app where the User can create Forms. I've already declared that user has_many forms and forms belongs_to user.

In addition, Forms have a nested model Item which stands for the questions of the Form, allowing a User to make a Form to have multiple questions. Also, Item has_many Answers so one question can have multiple answers (like radio or checkbox type of question). Finally, an Answer has_many Results allowing to store multiple values later for each answer if it is submited by different people.

Users also have Children which I'd like them to apply the Forms that they create.

How can I achieve this? Would you recommend to create an association between my Child model and Form model?

How would you do it?


So all I need is to have the forms created by one user available to be answered for each one of the user's children.


Update:

So I came up with the following idea:

Create a join table between form and children to map which forms were applied to which child.

Also, the result model should have an answer_id column of the answer which it responded to. And I guess it will need another column to point the id of the join table above to map which form-child belongs to.

¿Do you think this logic is right?


When displaying the form to be used, I plan to display form.title, loop into their items through each item and display its item.title and again loop into its answers through each answer and display its type and label as of the type of field and label for the form_fields of each of the results.

The problem now is how can I manage to do this? I know how to create and save specific number of Objects but not Dynamic number of Objects.

Code:

form.rb

belongs_to :user
has_many :items, dependent: :destroy
accepts_nested_attributes_for :items, allow_destroy: true

item.rb

belongs_to :form
has_many :answers, dependent: :destroy
accepts_nested_attributes_for :answers, allow_destroy: true

answer.rb

belongs_to :item

Upvotes: 0

Views: 151

Answers (2)

Hieu Pham
Hieu Pham

Reputation: 6707

Let me point out what may be the problems in your current schema:

Answer has_many Results allowing to store multiple values later for each answer if it is submited by different people

=> What happens if we want to query how many correct answers in Form for a specific User or how many correct answers in total for a Form?

Users also have Children which I'd like them to apply the Forms that they create

=> This is kind of confusing, between User and Form we have the relations: User can create many Forms and User can apply many Form via a model Children

I have researched on kind of this problem before, here is my suggested schema:

enter image description here

Explanation

  • User has many Form by creating action
  • Form has many Item
  • Item has many Question
  • Question has many Answer (An answer is correct/incorrect)

  • User applies 1/many Form, each time like that, an AnswerSheet was created between User and Form

  • AnswerSheet contains many SelectedAnswer so SelectedAnswer belongs to AnswerSheet and original Answer. Additionally, a SelectedAnswer was created by the action that user selects an Answer

This is clearer, we can explain the schema with the real meaning, hence, it is easier to understand & implement

One note I use AnswerSheet instead of your idea of Children, I think that makes sense, you can any term you want, but just make sure people understand it :)

Upvotes: 1

Atul Shukla
Atul Shukla

Reputation: 171

According to your Scenario ,I would like to recommend you there is many to many relationship between children and form and also child has_many results.

Upvotes: 0

Related Questions