Domanda

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?

È stato utile?

Soluzione

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
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top