I have a many-to-many relation that uses an intermediate join model and the :through option of has_many, like this ModelA has_many ModelAB has_many ModelB :through ModelAB ModelB has_many ModelAB has_many ModelA :through ModelAB ModelAB belongs_to ModelA belongs_to ModelB Now it is easy to find all ModelB objects that are connected to some ModelA object. But what is the best way to find all ModelB objects that are NOT connected to some specific ModelA object? What is the usual pattern to let the user add new ModelA objects to some ModelB object? I am considering to show a list of objects that are not yet added and let them select the ones to be added via some checkbox. So how can I find the set of ModelA objects not connected to the current ModelB object (or vice versa)? -- 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 -~----------~----~----~----~------~----~------~--~---
For example, you can use find_by_sql method (if you have a corresponding tables model_a, model_b, model_ab): ... a_without_b = ModelA.find_by_sql "SELECT model_a.* FROM model_a, model_ab WHERE model_a.id = model_ab.model_a_id AND model_ab.model_b_id = NULL" ... ---------------------- Best Regards, Sergey --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
priit.tamboom-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Nov-03 09:09 UTC
Re: Finding rows not connected through :though
If you like to study more about this topic then I recommend to read: http://blog.hasmanythrough.com/articles/category/associations It helps a lot. Priit --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---