Rails 3——需要帮助理解如何使用WHERE (XXXXX)查询

时间:2023-01-16 02:05:55

I want to build a rails query like the following. And would like to learn how so I can do it again on my own.

我想构建一个rails查询,如下所示。我想学习如何自己再做一次。

Pseudo Code:

伪代码:

@usersprojects = ?? Do I get record or just objects here? not sure?

@usersprojects = ? ?这里是记录还是对象?不确定吗?

SELECT * FROM audit_log WHERE project_id IN (@usersprojects)

从audit_log中选择*,其中project_id在(@usersprojects)

IN the where IN () do I some how tell Rails to use the record.id?

在where IN()中,我是否知道如何告诉Rails使用record.id?

thank you so much for helping me learn this. I want to Rails!

非常感谢你帮助我学习这个。我想Rails !

2 个解决方案

#1


3  

@kchau's answer was close, but you need to map out the project_id's from those records, like so:

@kchau的答案很接近,但是您需要从这些记录中绘制project_id,如下所示:

AuditLogs.find(@userprojects.map(&:project_id))

#2


0  

So, if @usersprojects contained the User's Projects that you're trying to find audit logs for, you would do a query like:

因此,如果@usersprojects包含了您试图查找审计日志的用户项目,您将执行如下查询:

    logs = AuditLogs.find(@userprojects)

See the Rails guide for reference.

参阅Rails指南以供参考。

#1


3  

@kchau's answer was close, but you need to map out the project_id's from those records, like so:

@kchau的答案很接近,但是您需要从这些记录中绘制project_id,如下所示:

AuditLogs.find(@userprojects.map(&:project_id))

#2


0  

So, if @usersprojects contained the User's Projects that you're trying to find audit logs for, you would do a query like:

因此,如果@usersprojects包含了您试图查找审计日志的用户项目,您将执行如下查询:

    logs = AuditLogs.find(@userprojects)

See the Rails guide for reference.

参阅Rails指南以供参考。