Hi all, This forum has been very helpful in the past so I figured I''d try and ask for some help on some (yet again) rather "newbie" stuff again. I currently have the following DB Schema (Two Tables Listed here): Users Table (user has_many children) ----------- id login ... Children Table (children belongs_to user) -------------- id first_name last_name is_lost (boolean) Users Controller show method: def show @user = User.find_by_login(params[:id]) @lost_children = @user.children.find(:all, :conditions => "is_lost ''t''") ... end This WORKS. However, I''m sure I can cut down doing a find query by doing something like this: @lost_children = @user.children(:is_lost => true) The above doesn''t work for me (as I just made it up out of the blue). But there must be something of this nature that can aid in avoiding making another DB call. I''ve tried searching for a while now but have had little luck. At the very least, any suggestions on how to make that code cleaner/better? Many thanks in advance! -Tony P.S. Please let me know if you need more code or explanations. -- 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 -~----------~----~----~----~------~----~------~--~---
Franz Strebel
2008-Oct-14 13:34 UTC
Re: Trying to replace some .find calls with better code
How about looking into named_scope. in your Child model: named_scope :lost, :conditions => "is_lost = ''t''" You can then do this: @user = User.find_by_login(params[:id]) @lost_children = @user.children.lost /franz --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Franz Strebel
2008-Oct-14 13:35 UTC
Re: Trying to replace some .find calls with better code
Forgot to mention that named_scope is native Rails 2.1 or later. If you''re using an earlier version, you''ll need to install the has_finder plugin. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Try: @lost_children = @user.children.select {|c| c.is_lost} -Roy -----Original Message----- From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails-talk@googlegroups.com] On Behalf Of Tony Tony Sent: Tuesday, October 14, 2008 6:17 AM To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Subject: [Rails] Trying to replace some .find calls with better code Hi all, This forum has been very helpful in the past so I figured I''d try and ask for some help on some (yet again) rather "newbie" stuff again. I currently have the following DB Schema (Two Tables Listed here): Users Table (user has_many children) ----------- id login ... Children Table (children belongs_to user) -------------- id first_name last_name is_lost (boolean) Users Controller show method: def show @user = User.find_by_login(params[:id]) @lost_children = @user.children.find(:all, :conditions => "is_lost ''t''") ... end This WORKS. However, I''m sure I can cut down doing a find query by doing something like this: @lost_children = @user.children(:is_lost => true) The above doesn''t work for me (as I just made it up out of the blue). But there must be something of this nature that can aid in avoiding making another DB call. I''ve tried searching for a while now but have had little luck. At the very least, any suggestions on how to make that code cleaner/better? Many thanks in advance! -Tony P.S. Please let me know if you need more code or explanations. -- 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---