mig
2008-Jan-02 20:34 UTC
has_many_and_belongs_to preloads all data -> how can I do to not to do it?
Hello, I have two tables: class Person < ActiveRecord::Base has_and_belongs_to_many :projects end class Project < ActiveRecord::Base has_and_belongs_to_many :people end Then I want to get people working on a project. I do: project_record.people.find_all_by_foobar( foobar ) It works, but it internally loads ALL the people working on the project, not only "foobar" people. It seems that just using "project_record.people" loads everything into memory. Is there any workaround how to load only what is really needed? Lets imagine there is huge amount of projects and you just want to check if a person works on a certain project... Thank you, Jan --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Greg Donald
2008-Jan-02 22:42 UTC
Re: has_many_and_belongs_to preloads all data -> how can I do to not to do it?
On 1/2/08, mig <mig-Cu8T68VxBMo@public.gmane.org> wrote:> > Hello, > > I have two tables: > > class Person < ActiveRecord::Base > has_and_belongs_to_many :projects > end > > class Project < ActiveRecord::Base > has_and_belongs_to_many :people > end > > Then I want to get people working on a project. I do: > > project_record.people.find_all_by_foobar( foobar ) > > It works, but it internally loads ALL the people working on the project, not > only "foobar" people. > > It seems that just using "project_record.people" loads everything into memory. > Is there any workaround how to load only what is really needed? Lets imagine > there is huge amount of projects and you just want to check if a person works > on a certain project...Have you considered something like this? project = Project.find( 1 ) person = People.find( 1 ) person.projects.include? project -- Greg Donald http://destiney.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 -~----------~----~----~----~------~----~------~--~---
Java
2008-Jan-03 00:14 UTC
Re: has_many_and_belongs_to preloads all data -> how can I do to not to do it?
Take a look at http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html For many-to-many associations, there are no dynamic finder proxy methods. Use project_record.people.find(:all, :conditions => { :foobar => "foobar" }) instead. On 2 Jan., 21:34, mig <m...-Cu8T68VxBMo@public.gmane.org> wrote:> Hello, > > I have two tables: > > class Person < ActiveRecord::Base > has_and_belongs_to_many :projects > end > > class Project < ActiveRecord::Base > has_and_belongs_to_many :people > end > > Then I want to get people working on a project. I do: > > project_record.people.find_all_by_foobar( foobar ) > > It works, but it internally loads ALL the people working on the project, not > only "foobar" people. > > It seems that just using "project_record.people" loads everything into memory. > Is there any workaround how to load only what is really needed? Lets imagine > there is huge amount of projects and you just want to check if a person works > on a certain project... > > Thank you, > Jan--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---