Given class Customer belongs_to :invoice_address, :class_name => "Address", ... belongs_to :delivery_address, :class_name => "Address", ... belongs_to :contact, :class_name => "Employee", ... end How would you go about filtering a find :all where e.g. invoice_address.city = "Oslo", delivery_address.zip = 1234 and contact.department = 14? Exactly which and how many fields we want to filter on is highly dynamic. Doing it in the db is a must. Suggestions? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
eager loading of associations should get in the right direction: Customer.find(:all, :include => [:invoice_address, :delivery_address, :contact], :conditions => ["Address.city=? AND Address.zip=? AND contact.department=?", "Oslo", 1234, 14]) but this example wouldn''t do exactly what you want, since it''s mixing invoice & delivery address. it will take some work, to set this up the right way, maybe get a working sql solution first. -- 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 -~----------~----~----~----~------~----~------~--~---
On Wed, Apr 9, 2008 at 3:19 PM, Thorsten Mueller <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > eager loading of associations should get in the right direction: > > Customer.find(:all, :include => [:invoice_address, :delivery_address, > :contact], :conditions => ["Address.city=? AND Address.zip=? AND > contact.department=?", "Oslo", 1234, 14]) > > but this example wouldn''t do exactly what you want, since it''s mixing > invoice & delivery address. > it will take some work, to set this up the right way, maybe get a > working sql solution first.We''re currently thinking of feeding :joins an sql fragment along the lines of: conditions.keys.substring(".") do |alias| "inner join <alias_to_table_name> as <alias> on (<alias>.id customer.<alias>_id)" end.join(" ") Which would use a condition hash like: {"invoice_address.city" => "Oslo", "delivery_address.zip" => "1234", ...} Unless someone can come up with a better approach. --~--~---------~--~----~------------~-------~--~----~ 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 must be missing something. Why can''t you simply eager-load the associations and do your "invoice_address.city" => "Oslo" type conditions? Julian. Learn Ruby on Rails! Check out the FREE VIDS (for a limited time) VIDEO #3 out NOW! http://sensei.zenunit.com/ On 10/04/2008, at 9:00 PM, Isak Hansen wrote:> > On Wed, Apr 9, 2008 at 3:19 PM, Thorsten Mueller > <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >> >> eager loading of associations should get in the right direction: >> >> Customer.find(:all, :include => [:invoice_address, :delivery_address, >> :contact], :conditions => ["Address.city=? AND Address.zip=? AND >> contact.department=?", "Oslo", 1234, 14]) >> >> but this example wouldn''t do exactly what you want, since it''s mixing >> invoice & delivery address. >> it will take some work, to set this up the right way, maybe get a >> working sql solution first. > > We''re currently thinking of feeding :joins an sql fragment along the > lines of: > conditions.keys.substring(".") do |alias| > "inner join <alias_to_table_name> as <alias> on (<alias>.id > customer.<alias>_id)" > end.join(" ") > > Which would use a condition hash like: > {"invoice_address.city" => "Oslo", "delivery_address.zip" => > "1234", ...} > > > Unless someone can come up with a better approach. > > >--~--~---------~--~----~------------~-------~--~----~ 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 11 Apr 2008, at 00:41, Julian Leviston wrote:> > I must be missing something. > > Why can''t you simply eager-load the associations and do your > "invoice_address.city" => "Oslo" type conditions?Because the table addresses is joined twice you''d need to know what it was aliased as. Fred> > > Julian. > > > Learn Ruby on Rails! Check out the FREE VIDS (for a limited time) > VIDEO #3 out NOW! > http://sensei.zenunit.com/ > > On 10/04/2008, at 9:00 PM, Isak Hansen wrote: > >> >> On Wed, Apr 9, 2008 at 3:19 PM, Thorsten Mueller >> <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >>> >>> eager loading of associations should get in the right direction: >>> >>> Customer.find(:all, :include => >>> [:invoice_address, :delivery_address, >>> :contact], :conditions => ["Address.city=? AND Address.zip=? AND >>> contact.department=?", "Oslo", 1234, 14]) >>> >>> but this example wouldn''t do exactly what you want, since it''s >>> mixing >>> invoice & delivery address. >>> it will take some work, to set this up the right way, maybe get a >>> working sql solution first. >> >> We''re currently thinking of feeding :joins an sql fragment along the >> lines of: >> conditions.keys.substring(".") do |alias| >> "inner join <alias_to_table_name> as <alias> on (<alias>.id >> customer.<alias>_id)" >> end.join(" ") >> >> Which would use a condition hash like: >> {"invoice_address.city" => "Oslo", "delivery_address.zip" => >> "1234", ...} >> >> >> Unless someone can come up with a better approach. >> >>> > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---