Sean
Sean

Reputation: 1098

has and belongs to many through

I have a complex relationship that I am having a hard time representing in the views. I need some help figuring out the best way to set up relationships and that will probably solve most of my problem.

I've got a diagram here: Diagram

Basically I am trying to set up a dynamic survey type of application. An admin can go and add question and question choices. When a user is logged in, they can go answer these questions. They can only choose one answer per question, but there will be a section where they can choose whatever answers they would accept from a 'match'. This will be used to match users.

For example:

Question:

What is your favorite color? A) Red B) Yellow C) Blue

Billy Chooses Red, but says he will accept Red or Yellow as answers for a match.

John Chooses Yellow and says he will accept Red or Yellow answers. So because they both chose colors that are in each others "accept list" then those two will match on questions.

What is a good way to both set up relationships and represent it in a view? I'm having a hard time wrapping my brain around this one...

Thanks!

Upvotes: 0

Views: 191

Answers (1)

Karol85
Karol85

Reputation: 275

I don't know whether you can change your schema but if yes, in my opinion you can do it simple.

I will try to do something like below :

Models(tables) :

Users
  -id

Questions
  -id

Choices
  -id
  -question_id

Answers
  -id
  -user_id
  -choice_id
  -question_id
  -is_acceptance (boolean)

User has_many answers
Qestion has_many choices
Choice belongs_to question
Answer bleongs_to user , question 

Then you can save all answers in one table and flag (true or false) only main answer. After all you can write your own logic to retrive same answers , acceptance answers etc ..

Upvotes: 1

Related Questions