Is HABTM the answer between the three classes?
Not. You do not need HABTM in any of these relationships.
- What is the correct connection between users and questions?
- What is the correct relationship between users and answers?
In both cases, this is a one-to-many relationship: the user has many questions, and the user has many answers.
From a logical point of view, consider this: One question can never be authorized by several users, and one answer cannot be authorized by several users. . As such, this is not a many-to-many relationship.
In this case, your classes should be configured as follows:
class User < ActiveRecord::Base has_many :questions has_many :answers end class Question < ActiveRecord::Base belongs_to :user has_many :answers end class Answer < ActiveRecord::Base belongs_to :user belongs_to :question end
If you, on the other hand, have a tag system similar to StackOverflow, you need a HABTM relationship. One question can have many tags, while one tag can have many questions. As a basic example, your post has three tags (ruby-on-rails, habtm, external relations), while the ruby-on-rails tag has 8,546 questions.
vonconrad
source share