I have an incident model and a pupil model, and they have a many to many relationship through an involvement model. Also, a pupil belongs to a cohort. How can I show incidents (ideally just once), in date order, where there is an involvement present from a pupil where their pupil.cohort_id == 1 ? At the moment, I''m finding all pupils where pupil.cohort_id ==1, then iterating through the @pupils, listing the involvements/incident details for those pupils. But this isn''t great, as (i) incidents show up more than once where two pupils are involved in the incident, and (ii) I can''t order by incident date. It feels backwards. I suspect maybe I can use @incidents = Incident.find(:all) @incidents = @incidents.select {|i| ... something something to do with involvements then pupil.cohort_id == 1? Maybe I''m miles off? Help or clues appreciated. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2009-Mar-01 19:37 UTC
Re: Find or select across a many to many relationship
On Mar 1, 5:52 pm, johnsonmlw <johnson...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have an incident model and a pupil model, and they have a many to > many relationship through an involvement model. Also, a pupil belongs > to a cohort. > > How can I show incidents (ideally just once), in date order, where > there is an involvement present from a pupil where their > pupil.cohort_id == 1 ?One way would be something like Incident.find :all, :joins => :pupil, :conditions => ["pupils.cohort_id = ?", 1] This joins the pupils table (AR is smart enough to know that in needs to join the involvements tables as well) and then you can easily apply conditions on pupils order stuff etc... You''ll need to put a distinct in there not to get duplicate rows.> > At the moment, I''m finding all pupils where pupil.cohort_id ==1, then > iterating through the @pupils, listing the involvements/incident > details for those pupils. But this isn''t great, as (i) incidents show > up more than once where two pupils are involved in the incident, and > (ii) I can''t order by incident date. It feels backwards. > > I suspect maybe I can use > > @incidents = Incident.find(:all) > @incidents = @incidents.select {|i| ... something something to do with > involvements then pupil.cohort_id == 1?Anything that starts with Foo.find(:all) will run out of steam when there starts to be a large number of foos. Fred> > Maybe I''m miles off? > > Help or clues appreciated.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Oh yes. Very nice. Thanks. AR is very aware! Clever AR! -- Matt On Mar 1, 7:37 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Mar 1, 5:52 pm, johnsonmlw <johnson...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I have an incident model and a pupil model, and they have a many to > > many relationship through an involvement model. Also, a pupil belongs > > to a cohort. > > > How can I show incidents (ideally just once), in date order, where > > there is an involvement present from a pupil where their > > pupil.cohort_id == 1 ? > > One way would be something like Incident.find :all, :joins > => :pupil, :conditions => ["pupils.cohort_id = ?", 1] > This joins the pupils table (AR is smart enough to know that in needs > to join the involvements tables as well) and then you can easily apply > conditions on pupils order stuff etc... > > You''ll need to put a distinct in there not to get duplicate rows. > > > > > At the moment, I''m finding all pupils where pupil.cohort_id ==1, then > > iterating through the @pupils, listing the involvements/incident > > details for those pupils. But this isn''t great, as (i) incidents show > > up more than once where two pupils are involved in the incident, and > > (ii) I can''t order by incident date. It feels backwards. > > > I suspect maybe I can use > > > @incidents = Incident.find(:all) > > @incidents = @incidents.select {|i| ... something something to do with > > involvements then pupil.cohort_id == 1? > > Anything that starts with Foo.find(:all) will run out of steam when > there starts to be a large number of foos. > > Fred > > > > > Maybe I''m miles off? > > > Help or clues appreciated.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---