문제

I have three models: Question, User and Qrecord. A Qrecord stores how many times the User has answered the Question correctly (in the column :answered) and when it was last answered (in the column :last_answered).

So, a Question has_many Qrecords and a User has_many Qrecords. A Qrecord belongs_to :user and :question.

Qrecord Model

id            :integer          not null, primary key
user_id       :integer
question_id   :integer
answered      :integer
last_answered :datetime

For an array of questions, how do I return the one in which :last_answered was the longest time ago?

도움이 되었습니까?

해결책

Assuming that the array of questions is questions and the user record user:

user.qrecords.where(question_id: questions.pluck(:id)).order(:last_answered).last.question
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top