Rails 3 – Need help understanding how to QUERY with a WHERE IN (XXXXX) – Education Career Blog

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 just 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.

Leave a Comment