How to setup nested models with Rails 4 - ruby-on-rails

I'm using Rails 4 to build a question and answers type of application.
Should the models I create be:
Question
belongs_to :user
has_many :answers
Answer
belongs_to :user
belongs_to :question
Or should it be:
Question
QuestionAnswer

It's based on your preference on choosing the names of the tables, but having simply questions and answers tables are readable and makes sense when you call associated methods like say Question.last.answers rather than Question.last.question_answers (no duplicates).
Furthermore, QuestionAnswer looks more like something you use to represent a join model question_answers on a has_many through association.
# models/question.rb
# database table - questions
class Question < ActiveRecord::Base
belongs_to :user
has_many :answers
end
# models/answer.rb
# database table - answers (question_id as foreign key)
class Answer < ActiveRecord::Base
belongs_to :questions
end
However, it is really up to you how you want to model your data.

Related

How to model a Question and Answer data structure in ActiveRecord?

This feels like it should be super simple, but for the life of me I haven't been able to get it right.
In my application, I want to have Questions and Answers.
A Question can only have 1 Answer, but an Answer can be used for many Questions.
For example.
Question Table Data
"Two plus Two equals?"
"Number of sides to a square?"
Answer Table Data
Four
Both Questions only have 1 Answer, but the Answer record can be used for both Questions.
I thought maybe this would work ::
rails g resource question verbiage:string answer:references
but then I have to put a belongs_to :answer on the Question model and that doesn't seem right.
It feels like it should be possible to do something like ::
Question.first.answer # returns the single answer
Answer.first.questions # returns all of the Questions where this record is the Answer
Can anyone educate me on the proper way to model this in ActiveRecord?
You need has_many assosiaction.
I'm gonna use scaffold for this.
Create Answer:
rails g scaffold Answer value:string
Create Question:
rails g scaffold Question verbiage:string answer:references
Run rails db:migrate
Create assosiactions.
class Answer < ApplicationRecord
has_many :questions
end
class Question < ApplicationRecord
belongs_to :answer
end
If you don't want to use demir's answer of
question belongs_to answer
answer has_many question
You'll have to
Make a QuestionAnswer join table
QuestionAnswer belongs_to question
QuestionAnswer belongs_to answer
question has_one question_answers
answer has_many question_answers
Then
question has_one :answer through :question_answer source :answer
answer has_many :question through :question_answer source :question
Ex. https://guides.rubyonrails.org/association_basics.html#choosing-between-belongs-to-and-has-one
For example, it makes more sense to say that a supplier owns an account than that an account owns a supplier. This suggests that the correct relationships are like this
This really depends on what the requirements are. In most cases you actually need a join table:
class Question
has_many :options
has_many :answers, through: :options
end
class Option
belongs_to :question
belongs_to :answer
end
class Answer
has_many :options
has_many :questions, through: :options
end
answers = [Answer.create(verbiage: 'Dailey'), Answer.create(verbiage: 'Once a week'), Answer.create(verbiage: 'Never')]
question = Question.create(verbiage: 'How often do you drink milk?', answers: answers)
question_2 = Question.create(verbiage: 'How often do you excercise?', answers: answers)
If the question is has a correct answer you can use a separate association which is a direct link to the the answers table:
class Question
has_many :options
has_many :answers, through: :options
belongs_to :correct_answer, class_name: 'Answer'
end
Or you can add a boolean column to the options table if their can be multiple correct answers.
class Question
has_many :options
has_many :answers, through: :options
has_many :correct_answers, through: :options,
class_name: 'Answer',
-> { where(options: { correct: true }) }
end

Manage multiple association of two classes in rails

I want to manage the association of one to many type, for user and answers and association of many to many type, for vote management for the same two models i.e users and answers.
So, how to maintain both the associations at the same time?
This code is something that I want to implement.
class User < ActiveRecord::Base
has_many :answers #For the answers of particular user
has_and_belongs_to_many :answers #For the answers upvoted by a particular user
end
class Answer < ActiveRecord::Base
belongs_to :user #Author of the answer
has_and_belongs_to_many :users #For those who upvoted the answer
end
You should give a different name to the other association, but then you would also need to specify class name.
has_many :answers
has_and_belongs_to_many :voted_answers, class_name: "Answer"

Rails - track user's "actions" with relationship models

I'm building a math exercise app. I want to store questions that are separated into exercises and each exercise belongs to a different section. Also there are multiple sections for each grade (12 grades in total).
How do i track a user's answer (action)? For instance, when a user browses the app, decides to answer question 7 from exercise 'G1' in section 'G' of Grade 'Pre-K' and provides a correct answer i want to track that and in the future provide him with statistics about his performance.
Currently there isn't a connection between my User model to the questions and i wanted to hear from others as to what would be the most efficient way to tackle this (maybe the models right now are completely wrong)
This is the model diagram for now with has_many, belongs_to relationship (i'll add 'the_question':string and 'answer':string later on to the question model):
I think all that is missing is a Answer model, that is connected to the User.
Optionally, you can define the relations on the "parents" of Question too.
class Grade < ActiveRecord::Base
has_many :users, :through => :sections # optional. i.e. 'users that have answers in this grade'
end
class Section < ActiveRecord::Base
has_many :users, :through => :exercises # optional
end
class Exercise < ActiveRecord::Base
has_many :users, :through => :questions # optional
end
class Question < ActiveRecord::Base
has_many :answers
has_many :users, :through => :answers # optional
end
class Answer < ActiveRecord::Base
belongs_to :question
belongs_to :user
end
class User < ActiveRecord::Base
has_many :answers
end
Update: re-reading your description, I think CorrectAnswer would be better suitable than Answer.

Modeling a Poll Feature

I'm developing a poll feature that allows users to create poll questions with options and allow other users to answer them
The answer model includes an option_id column but the two models are not related.
I have two questions:
Do my models (below) fully encapsulate what I'm trying to represent?
How would the create method for an answer controller action look? (Specifically, how to retrieve the option_id)
Note: I've never dealt with this pattern in which a model(answer) references another model (option) via option_id but the two aren't related
So far I have this:
class Question < ActiveRecord::Base
belongs_to :user
has_many :options
has_many :answers
end
class Option < ActiveRecord::Base
belongs_to :question
end
class Answer < ActiveRecord::Base
belongs_to :user
belongs_to :question
end
Found a tut for a mobile app doing this

Can a single model "belong_to" more than one parent model?

Just as on StackOverflow, in my app a User can write Questions and can also provide Answers:
class User < ActiveRecord::Base
has_many :questions
has_many :answers
end
class Question < ActiveRecord::Base
has_many :answers
belongs_to :user
end
class Answer < ActiveRecord::Base
belongs_to :question
belongs_to :user
end
My question has to do with the Answer model above:
Is it ok for an Answer to belong_to both the User and the Question models?
I have a feeling I read somewhere that a model can only have a single foreign key. If so, how do I rectify that?
Yes, it is perfectly ok and you will have many models that have many belongs_to as your domain model gets more complex. I don't know where you would have read that a model can only have a single foreign key.

Resources