简体   繁体   English

Rails 4-如果在另一个表中不是特定记录,如何从一个表中选择数据?

[英]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.

相关问题 如何通过另一个表的订单记录从mysql表中选择日期 - How to select date from mysql table by order record of another table 从表中选择数据并从另一个表中填充特定值 - select data from a table and fill specific value from another table 如果在另一个表中找到记录,请从表中选择 - Select from table if record found in another table 如何从另一个表中选择表中的特定条件 - how to select a specific condition in table from another table 如何从表中选择记录,其中记录具有与另一个表最相似的属性 - how to select record from table where record have the most similar attribute from another table SQL:如何从联接到另一个表中选择最后插入的记录 - SQL : how to select the last inserted record from a table joined to another 如何从另一个表的多个记录中选择一个记录? - how to select one record from multi records of another table? Codeigniter查询:当我使用sum Select函数从另一个表中获取数据时,如何从表中的列中获取所有记录 - codeigniter query: how to take all the record from a column in a table when i use sum Select function to get data from another table 如何从表中选择数据,该数据是另一个表中的行数据 - how to select data from table which is a row data in another table 如何在一条记录中选择表A的数据,表B中的附加列填充了表C中的数据 - How to select data of table A in one record with additional columns from table B filled with data from table C
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM