I have the following associations:
class Question < ApplicationRecord
belongs_to :shopper
has_many :question_messages, dependent: :destroy
end
class QuestionMessage < ApplicationRecord
belongs_to :question
belongs_to :shopper, optional: true
belongs_to :store, optional: true
end
As you can see from the associations above I have two different user types(store, shopper) that can create question messages. I have an index page for the shopper user where I want to sort his questions according to the last question message he created. I'm trying this with the following code:
@questions = Question.where(shopper_id: current_shopper)
.group(:id).joins(:question_messages)
.order('max(question_messages.created_at) asc')
The issue with this code is that it sorts the questions according to last questions messages created and not specifically the last question message created by the shopper. Any ideas on how to implement this so I can sort the questions according to the last question message that was created by the shopper?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…