开发者

Rails 3 - Need help understanding how to QUERY with a WHERE IN (XXXXX)

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.

Pseudo Code:

@usersprojects = ?? Do I get record or ju开发者_运维技巧st objects here? not sure?

SELECT * FROM audit_log WHERE project_id IN (@usersprojects)

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

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


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

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


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

    logs = AuditLogs.find(@userprojects)

See the Rails guide for reference.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜