OK, for the life of me I can''t see anywhere how I am supposed to figure out if a record was actually found. class Shape < ActiveRecord::Base def self.findSquare find(:first, conditions: whetever.......) # HOW DO AI KNOW ONE WAS ACTUALLY FOUND? # SAME FOR find :all ?? end end -- gw --~--~---------~--~----~------------~-------~--~----~ 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 Willits wrote:> OK, for the life of me I can''t see anywhere how I am supposed to > figure out if a record was actually found. > > class Shape < ActiveRecord::Base > > def self.findSquare > find(:first, conditions: whetever.......) > > # HOW DO AI KNOW ONE WAS ACTUALLY FOUND? > # SAME FOR find :all ?? > > end > end > > -- gwclass Shape < ActiveRecord::Base def self.findSquare results = find(:first, conditions: whetever.......) if results.length > 0 results else raise "No hits!" end end end Is this what you want? Semin -- 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 Nov 1, 2007, at 5:45 PM, Semin Lee wrote:> Greg Willits wrote: >> OK, for the life of me I can''t see anywhere how I am supposed to >> figure out if a record was actually found. >> >> class Shape < ActiveRecord::Base >> >> def self.findSquare >> find(:first, conditions: whetever.......) >> >> # HOW DO AI KNOW ONE WAS ACTUALLY FOUND? >> # SAME FOR find :all ?? >> end >> end > > class Shape < ActiveRecord::Base > > def self.findSquare > results = find(:first, conditions: whetever.......) > if results.length > 0 > results > else > raise "No hits!" > end > end > end > > Is this what you want?Length is not an available method, nor size, nor count, nor anything else that would seem logical or semi-logical. -- gw --~--~---------~--~----~------------~-------~--~----~ 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 Nov 1, 2007, at 8:45 PM, Semin Lee wrote:> Greg Willits wrote: >> OK, for the life of me I can''t see anywhere how I am supposed to >> figure out if a record was actually found. >> >> class Shape < ActiveRecord::Base >> >> def self.findSquare >> find(:first, conditions: whetever.......) >> >> # HOW DO AI KNOW ONE WAS ACTUALLY FOUND? >> # SAME FOR find :all ?? >> >> end >> end >> >> -- gw > > class Shape < ActiveRecord::Base > > def self.findSquare > results = find(:first, conditions: whetever.......) > if results.length > 0 > results > else > raise "No hits!" > end > end > end > > Is this what you want? > > SeminWell, not quite. find(:first) is going to be nil or a Shape object. If it''s nil, you can''t call nil.length and if it''s a Shape object, then Shape#length might actually be something else. if result = find(:first, :conditions => ...) # do something with the record in result end result_array = find(:all, :conditions => ...) unless result_array.empty? # do something with the records in the result_array end for either case, you might just return the find() and let the caller check for nil or iterate over the empty array (Even if find(:all, ...) only gets a single match, it will still be in an array.) -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
And if there are no matches, it will be an empty array. If you need to do something with it, try this: Blah.find(:all).each do |blah| # Do something end On Nov 1, 7:21 pm, Rob Biedenharn <R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org> wrote:> On Nov 1, 2007, at 8:45 PM, Semin Lee wrote: > > > > > Greg Willits wrote: > >> OK, for the life of me I can''t see anywhere how I am supposed to > >> figure out if a record was actually found. > > >> class Shape < ActiveRecord::Base > > >> def self.findSquare > >> find(:first, conditions: whetever.......) > > >> # HOW DO AI KNOW ONE WAS ACTUALLY FOUND? > >> # SAME FOR find :all ?? > > >> end > >> end > > >> -- gw > > > class Shape < ActiveRecord::Base > > > def self.findSquare > > results = find(:first, conditions: whetever.......) > > if results.length > 0 > > results > > else > > raise "No hits!" > > end > > end > > end > > > Is this what you want? > > > Semin > > Well, not quite. find(:first) is going to be nil or a Shape object. > If it''s nil, you can''t call nil.length and if it''s a Shape object, > then Shape#length might actually be something else. > > if result = find(:first, :conditions => ...) > # do something with the record in result > end > > result_array = find(:all, :conditions => ...) > unless result_array.empty? > # do something with the records in the result_array > end > > for either case, you might just return the find() and let the caller > check for nil or iterate over the empty array > > (Even if find(:all, ...) only gets a single match, it will still be in > an array.) > > -Rob > Rob Biedenharn http://agileconsultingllc.com > R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Rob Biedenharn wrote:> On Nov 1, 2007, at 8:45 PM, Semin Lee wrote: >>> # SAME FOR find :all ?? >> if results.length > 0 >> results >> else >> raise "No hits!" >> end >> end >> end >> >> Is this what you want? >> >> Semin > > > Well, not quite. find(:first) is going to be nil or a Shape object. > If it''s nil, you can''t call nil.length and if it''s a Shape object, > then Shape#length might actually be something else. > > if result = find(:first, :conditions => ...) > # do something with the record in result > end > > result_array = find(:all, :conditions => ...) > unless result_array.empty? > # do something with the records in the result_array > end > > for either case, you might just return the find() and let the caller > check for nil or iterate over the empty array > > (Even if find(:all, ...) only gets a single match, it will still be in > an array.) > > -Rob > Rob Biedenharn http://agileconsultingllc.com > Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.orgoops, sorry I misunderstood the return types of find(:all) and find(:first). thanks Rob, Semin -- 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 Nov 1, 2007, at 6:21 PM, Rob Biedenharn wrote:> Greg Willits wrote: >> OK, for the life of me I can''t see anywhere how I am supposed to >> figure out if a record was actually found. >> >> class Shape < ActiveRecord::Base >> def self.findSquare >> find(:first, conditions: whetever.......) >> >> # HOW DO AI KNOW ONE WAS ACTUALLY FOUND? >> # SAME FOR find :all ?? >> end >> end> find(:first) is going to be nil or a Shape object. > If it''s nil, you can''t call nil.length and if it''s a Shape object, > then Shape#length might actually be something else. > > if result = find(:first, :conditions => ...) > # do something with the record in result > end > > result_array = find(:all, :conditions => ...) > unless result_array.empty? > # do something with the records in the result_array > end > > for either case, you might just return the find() and let the caller > check for nil or iterate over the empty array > > (Even if find(:all, ...) only gets a single match, it will still be in > an array.)I noodled with something like that but ran into problems with subsequent steps which got me sidetracked. I now have that sorted out. I apparenty had my head warped by the combination of what Rails was doing, Ruby''s implied return, and the class vs instance scoping of methods and my own arbitrary attributes added to the model. My son managed to unwarp all that, and now I can see again. Thx. -- gw --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---