views:

23

answers:

1

Hello,

For a user I have the following as an example:

[#<Permission id: 1, project_id: 3, role_id: 1, user_id: 1>, #<Permission id: 43, project_id: 2, role_id: 1, user_id: 1>, #<Permission id: 44, project_id: 4, role_id: 1, user_id: 1>, #<Permission id: 45, project_id: 5, role_id: 2, user_id: 1>, #<Permission id: 46, project_id: 6, role_id: 3, user_id: 1>, #<Permission id: 47, project_id: 7, role_id: 1, user_id: 1>]

I'm able to do this:

<% if results.permissions.empty? %>

Which tells me if the results.permissions has 1 or more records.

But what I'd like to do is something like this:

<% if results.permissions.where(spaceid = 3).empty? %> 

What I'm trying to do is take a project_id variable and filter that with the results.permission object.

Suggestions?

Thanks!

+4  A: 

If you are using a :has_many association you should be able to do the following:

results.permissions.find_by_project_id(3)

or if you want to get all the records with that id, you could use:

results.permissions.find_all_by_project_id(3)
Pete
@pete. thanks but I already did the query against the database. I want to do this type of filter in the partial. is that possible?
AnApprentice
oh wow, that worked. Thank you!
AnApprentice