Scope has_many relationship
I have four models
class Problem < ApplicationRecord has_many :questions end
class User < ApplicationRecord has_many :questions has_many :replies end
class Question < ApplicationRecord belongs_to :problem belongs_to :user has_many :replies end
class Reply < ApplicationRecord belongs_to :user belongs_to :question end
The Reply model has an attribute answer:boolean, a question can have multiple replies and those replies can be marked as an answer to their associated question.
I am trying to create two scopes on the Question model one for answered questions and one for unanswered questions.
answered -> has at least one reply that was marked as an answer (boolean is true)
unanswered -> has no replies marked as an answer (boolean is true)
I would like to be able to use the scopes like this:
Also I will be displaying the user associated with the questions as well as the replies associated with the questions and the users associated with those replies, so think I need to use some type of "includes" statement so that I don't create N + 1 query.
I am stuck on how to achieve this?