Hi all! I have this declaration model: http://pastie.caboo.se/83840 What I want to do: get all users that not belong to the declaration. I could do a loop. First get all the users. I know who belong to the declaration, so I also know who does not. But this seems like a lot of looping. Is there an easy way I can get all those users that NOT belong to the declaration? I looked into using joins in my find, but I don''t know how to use that in this context. Thanks in advance! -- 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 -~----------~----~----~----~------~----~------~--~---
I can only think of doing it in Ruby:
def other_users
User.find(:all) - self.users
end
there must be a more efficient way to do it via ActiveRecord
(therefore, only one query), something like
def other_users
User.find :all, :conditions => ["declaration_id <> ?",
self.id]
end
(or something like that, you get the idea, it''s awfully not
abstracted).
Bye,
nachokb
On Jul 31, 5:33 pm, Leon Bogaert
<rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
wrote:> Hi all!
>
> I have this declaration model:http://pastie.caboo.se/83840
>
> What I want to do:
> get all users that not belong to the declaration.
>
> I could do a loop. First get all the users. I know who belong to the
> declaration, so I also know who does not.
> But this seems like a lot of looping.
>
> Is there an easy way I can get all those users that NOT belong to the
> declaration? I looked into using joins in my find, but I don''t
know how
> to use that in this context.
>
> Thanks in advance!
> --
> Posted viahttp://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
-~----------~----~----~----~------~----~------~--~---
Leon, I think a left outer join of Users on Declarations would do it. in SQL that would be: SELECT * from Users left outer join Declarations ON Users.user_id Declarations.user_id WHERE Declarations.user_id IS NULL in ActiveRecord, an :includes does a left outer join so (I may not get this 100% right) User.find(:all, :conditions => "declarations.user_id is NULL", :include => "declarations") Cheers, --Kip On Aug 1, 4:33 am, Leon Bogaert <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi all! > > I have this declaration model:http://pastie.caboo.se/83840 > > What I want to do: > get all users that not belong to the declaration. > > I could do a loop. First get all the users. I know who belong to the > declaration, so I also know who does not. > But this seems like a lot of looping. > > Is there an easy way I can get all those users that NOT belong to the > declaration? I looked into using joins in my find, but I don''t know how > to use that in this context. > > Thanks in advance! > -- > Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---