[英]Rails 4 - How to select data from a table if in another table is not a specific record?
I have this model schedule: 我有这个模型时间表:
class Question < ActiveRecord::Base
has_many :closed_questions
end
class ClosedQuestion < ActiveRecord::Base
belongs_to :question
belongs_to :user
end
And I am trying to fetch all questions for a user that this user didn't check as closed. 我正在尝试为该用户未检查为已关闭的用户获取所有问题。
Example: 例:
ID | Question
1 | Question A
2 | Question B
3 | Question C
4 | Question D
User 1
checked question with ID 3
as closed. User 1
已关闭ID为3
问题。 How to get the output of question IDs 1, 2, 4
? 如何获取问题ID 1, 2, 4
的输出?
Thank you in advance. 先感谢您。
You may want to try something like this: 您可能要尝试这样的事情:
user = User.find(1)
Question.where.not(id: user.closed_questions.pluck(:question_id))
Note that prior to Rails 4 you might see this written as: 请注意,在Rails 4之前,您可能会看到以下内容:
user = User.find(1)
Question.where("id NOT IN (?)", user.closed_questions.pluck(:question_id))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.