I''m not quite sure how to get at this data using Actrive record.. I''m trying to get all the appointments made for a particular project. I''m using four models, Projects, Tasks, Resources and Appointments. The SQL gets me what I''m looking for but how do I do it using Active Record? Select a.* from appointments a join resources r on r.id = a.resource_id join tasks t on t.id = r.task_id join projects p on p.id = t.project_id Where project_id = 1; I can get all the Resources for a project using this but can not get the appointments. I could loop through the resources getting all the appointments and tracking them, but that doesn''t seem like the right way to do it. I could also use find_by_sql, but again, that does''t seem right. class Project < ActiveRecord::Base has_many :tasks belongs_to :customer has_many :resources, :through => :tasks Any help would be greatly 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 -~----------~----~----~----~------~----~------~--~---
Mark Studebaker wrote:> I''m not quite sure how to get at this data using Actrive record.. I''m > trying to get all the appointments made for a particular project. I''m > using four models, Projects, Tasks, Resources and Appointments. > > The SQL gets me what I''m looking for but how do I do it using Active Record? > > Select a.* from appointments a > join resources r on r.id = a.resource_id > join tasks t on t.id = r.task_id > join projects p on p.id = t.project_id > Where project_id = 1;Why is project joined in, if project_id comes from tasks? And why is resources joined in? for its data? It does not participate in the links, right?> I can get all the Resources for a project using this but can not get the > appointments. I could loop through the resources getting all the > appointmen ts and tracking them, but that doesn''t seem like the right > way to do it. I could also use find_by_sql, but again, that does''t seem > right. > > class Project < ActiveRecord::Base > has_many :tasks > belongs_to :customer > has_many :resources, :through => :tasksAppointment.find( :all, :include => :tasks, :conditions => { :''tasks.project_id'' => 1} ) In summary, provide enough :has_many => :through to get to the keying table, then put it into your :include and :conditions. Warning: :include will eager-load, which might slow you down with irrelevant data. Alternately... Task.find_all_by_project_id(1).map(&:appointments).uniq (-: -- Phlip --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thanks Phlip - Thanks for the response - I''ll play around with your suggestions. What I was trying to do was something like this project.appointments to display all the appointments for a specific project. The way I have it set up is that a project has many tasks, each task can have many resources (people) and each resource then has appointments. I''m not sure this is the best way to model but I thought it would be easier just keeping one reference to the project_id in the tasks table rather then trying to manage all the links back up to the projects table from the resources and appointments tables. Maybe a bad design decision?? ----- Original Message ---- From: Phlip <phlip2005-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Sent: Saturday, September 6, 2008 8:52:49 PM Subject: [Rails] Re: Active record question Mark Studebaker wrote:> I''m not quite sure how to get at this data using Actrive record.. I''m > trying to get all the appointments made for a particular project. I''m > using four models, Projects, Tasks, Resources and Appointments. > > The SQL gets me what I''m looking for but how do I do it using Active Record? > > Select a.* from appointments a > join resources r on r.id = a.resource_id > join tasks t on t.id = r.task_id > join projects p on p.id = t.project_id > Where project_id = 1;Why is project joined in, if project_id comes from tasks? And why is resources joined in? for its data? It does not participate in the links, right?> I can get all the Resources for a project using this but can not get the > appointments. I could loop through the resources getting all the > appointmen ts and tracking them, but that doesn''t seem like the right > way to do it. I could also use find_by_sql, but again, that does''t seem > right. > > class Project < ActiveRecord::Base > has_many :tasks > belongs_to :customer > has_many :resources, :through => :tasksAppointment.find( :all, :include => :tasks, :conditions => { :''tasks.project_id'' => 1} ) In summary, provide enough :has_many => :through to get to the keying table, then put it into your :include and :conditions. Warning: :include will eager-load, which might slow you down with irrelevant data. Alternately... Task.find_all_by_project_id(1).map(&:appointments).uniq (-: -- Phlip --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Mark Studebaker wrote:> What I was trying to do was something like this project.appointments to > display all the appointments for a specific project.Briefly, since you seem to have more experience with raw SQL than ActiveRecord (better than the other way around!;), try inspect_sql to see what you are getting as you tune this. But I should let someone else take a crack at the data model question now. -- Phlip --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Sep 7, 1:52 am, Phlip <phlip2...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Appointment.find( :all, :include => :tasks, > :conditions => { :''tasks.project_id'' => 1} ) > > In summary, provide enough :has_many => :through to get to the keying table, > then put it into your :include and :conditions. > > Warning: :include will eager-load, which might slow you down with irrelevant data. >In a situation like this :joins might be more appropriate - it will write the joins but won''t do any of the stuff :include would Fred --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> In a situation like this :joins might be more appropriate - it will > write the joins but won''t do any of the stuff :include wouldCan you :joins => :leaf_records ? Or do you got to write all the raw SQL? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 7 Sep 2008, at 12:54, Phlip wrote:> >> In a situation like this :joins might be more appropriate - it will >> write the joins but won''t do any of the stuff :include would > > Can you :joins => :leaf_records ? Or do you got to write all the raw > SQL? >You can either pass raw sql or options in exactly the same format as include takes them (eg :joins => [{:foo => {:bar => [:baz, :bam]}}, :bim]) Fred> > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---