Can a single model "belong_to" more than one parent model? - ruby-on-rails

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.

Related

How to setup nested models with Rails 4

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.

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"

Designing Database for Long Forms in Rails & ActiveRecord

I want to store complex form/questionnaire data but I'm having a hard time designing the database:
A User can create a new Exam. When creating an Exam, the user needs to choose from a template with 30 pre-set questions (and I want to store the answer to each of these questions in the database).
Purpose of the template is so that practitioners might want to run different exams (e.g. knee exam, arm exam, spine exam etc). So they create a new exam, choose a specific template. And each template has many questions
What's the best way to design this database? I started creating Exam, Template, and Question models, but then I got lost building the relationships.
Here's how I ended up designing the database. I added a 4th "answers" model. This way, I can associate answers to questions, and questions to templates, and templates to exams.
Hope this helps someone
class User < ActiveRecord::Base
has_many :patients
has_many :assessments
end
class Patient < ActiveRecord::Base
belongs_to :user
has_many :assessments
end
class Assessment < ActiveRecord::Base
belongs_to :user
belongs_to :template
belongs_to :patient
has_many :questions, :through=> :template
has_many :answers, :through=> :questions
end
class Template < ActiveRecord::Base
belongs_to :user
has_many :questions
end
class Question < ActiveRecord::Base
belongs_to :template
has_one :answer
end
class Answer < ActiveRecord::Base
belongs_to :question
end

Saving and retrieving state in web forms

I'm creating a 'work book' ruby on rails application. Basically a user will read a paragraph or two and then answer some questions based on what they've read.
I'm unsure how to model this. The paper work books have many pages and each page has many paragraphs.
Should I model it exactly as it is in physical form or should I simplify the model?
My biggest question is how do I save state so that a user can update their workbook and or make corrections after they've submitted part of their assignment?
I think these relations could work for you:
class WorkBook
has_many :pages
class Page
belongs_to :work_book
has_many :paragraphs
class Paragraph
belongs_to :page
has_many :questions
class Question
belongs_to :paragraph
has_many :answers
class Answer
belongs_to :question
belongs_to :user
class User
has_many :answers
has_many :answered_questions, class_name: 'Question', through: :answers
If you want a better control on your answers (like value_string, value_float, value_boolean, etc.) I recommend you to add an extra model "AnswerValue".
You might feel like there is too much models in this case, but trust me, it makes your app flexible and allows you to add more "granular" features on each model / relation.
An extenstion with these relations: You could add a model "AnswerCorrection" which would act like an Answer but would be a way to save another value after the user update it's workbook.
Hope this helps!
I'd go for something like this:
class PaperWork < ActiveRecord::Base
has_many :pages
has_many :paragraphs, through: :pages
end
class Page < ActiveRecord::Base
belongs_to :paper_work
has_many :paragraphs
end
class Paragraph
belongs_to :page
belongs_to :paper_work, through: :page
end
Then you can model assignments somewhat around this idea:
class Asignment < ActiveRecord::Base
belongs_to :user
belongs_to :paragraph
# attributes: description, status(pending, completed, etc.)
end

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

Resources