Greg Hauptmann
2007-Jul-01 06:42 UTC
Can active record support: ''find'' parent objects, but filtered based on the contents of it''s associated child objects data
Hi, Can active record handle the following? Or do I need to stick to ".find_by_sql" to do this: select parent.id, parent.title, parent.description from parent, child where parent.id = child.parent_id AND child.memo like ''%texttosearch%'' That is, I want to effectively ''find'' parent objects, but filtered based on the contents of it''s associated child objects data. So the question is if it is possible what would the ruby code be to do this? i.e. results Parent.find(:all, ... Thanks Greg --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Matthew Rudy
2007-Jul-17 09:56 UTC
Re: Can active record support: ''find'' parent objects, but fi
if you take your query and break it up into the following sections [:select, :from, :conditions] then you''ll be able to do find(:all, :select => "parent.id, parent.title, parent.description", :from => "parent, child", :conditions => [ "parent.id = child.parent_id AND child.memo like ?", "%#{texttosearch}%" ] ) Greg Hauptmann wrote:> Hi, > > Can active record handle the following? Or do I need to stick to > ".find_by_sql" to do this: > > select parent.id, parent.title, parent.description from parent, child > where parent.id = child.parent_id AND child.memo like > ''%texttosearch%'' > > That is, I want to effectively ''find'' parent objects, but filtered based > on > the contents of it''s associated child objects data. So the question is > if > it is possible what would the ruby code be to do this? i.e. results > Parent.find(:all, ... > > Thanks > Greg-- 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 -~----------~----~----~----~------~----~------~--~---