Carmine Moleti
2008-Sep-25 09:22 UTC
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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Erol Fornoles
2008-Sep-25 09:38 UTC
Re: How to find records, placing conditions on joined table?
On Sep 25, 5:22 pm, Carmine Moleti <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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 > -- > Posted viahttp://www.ruby-forum.com/.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"]) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Carmine Moleti
2008-Sep-25 09:41 UTC
Re: How to find records, placing conditions on joined table?
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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---