How to find records, placing conditions on joined table?

Hi to everyone,

I have my User model which has_many roles.
Now, I've the need to find all users with certain roles.

At first I thought of something like

User.find(:all, :joins => :roles).select { |u| u.has_role?(:admin) }

But this seems unnatural to me, because it forces to load all the Users
and then applying a filter
on the collections.

Is there a way to obtain the same result in a better way?

Perhaps something like:

User.find(:all, :joins => :roles, :conditions => [ "'admin' in roles" ])

Thanks in advance for your help.

Regards,
Carmine

Assuming the reference field of Role is name:

User.find(:all, :joins => :roles, :conditions => ["roles.name IN (?)",
"admin"])

You can also do:

User.find(:all, :joins => :roles, :conditions => ["roles.name IN (?)",
["admin", "editor"])

Hi there Erol!

Thanks so much for your help!

User.find(:all, :joins => :roles, :conditions => ["roles.name IN (?)",
["admin", "editor"])

That's waaaay better!
Thank you