views:

26

answers:

2

Howdy.

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!

A: 

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.

kchau
Thank you. But that errord "Couldn't find all AuditLogs with IDs (#<Permission:0x107b34310>, #<Permission:0x107b341d0>, #<Permission:0x107b34108>, #<Permission:0x107b34018>, #<Permission:0x107b33f78>, #<Permission:0x107b33af0>, #<Permission:0x107b33960>, #<Permission:0x107b33848>, #<Permission:0x107b335c8>) (found 1 results, but was looking for 9)" The usersprojects object contains permission records like so (id, project_id, user_id). that's where I'm stuck.
+3  A: 

@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))
Jaime Bellmyer