It looks like a typo got introduced as you were moving the method into your rails app. There is no values_at call in my method, perhaps you accidentally tab completed and inadvertently introduced the _at. Good luck. Tim
On May 26, 2:07 am, Robert Scott <[email protected]> wrote: > Tim, > > Thanks again for the detailed thoughts. It looks like your approach is > structured in a way that should handle what I'm looking for, and provide > a framework for future expansion. I went ahead and implemented it in, > and am trying to resolve an issue now (NoMethodError (undefined method > `values_at') for `search_array_with_hashes' but I'll let you know once > it gets working. > > Cheers! > > > > > > timr wrote: > > Hi again Robert, > > There might be methods build into rails for doing this, but when you > > have a very specific case, you might just roll out your own methods to > > get exactly what you want: > > > =begin > > given a data structure like @contacts = > > [{:first_name=>'tim', :last_name=>'rand', :id = > > 1},{:first_name=>'jim', :last_name=>'band', :id => > > 2},{:first_name=>'him', :last_name=>'crand', :id => 3}] > > and given a query may be first, last, or both names > > return id number for matches > > =end > > > #here is our search array > > @contacts = [{:first_name=>'tim', :last_name=>'rand', :id => > > 1},{:first_name=>'jim', :last_name=>'band', :id => > > 2},{:first_name=>'him', :last_name=>'crand', :id => 3}, > > {:first_name=>'shim', :last_name=>'crand', :id => 4}] > > > #method to separate names if more than one is given > > def parse_query(query) > > if query.match(" ") > > name1, name2 = query.split(/ /) > > else > > name1 = query > > return name1.to_a > > end > > return [name1, name2] > > end > > > #find any name in hash field and return the ids > > def search_array_with_hashes(array_with_name_or_names) > > @hits = [] > > #search first names > > array_with_name_or_names.each do |name| > > @contacts.each do |hash| > > @hits << hash[:id] if hash.values.include?(name) > > end > > end > > @hits.uniq > > end > > > #usage/test case examples > > p search_array_with_hashes(parse_query("band")) > > p search_array_with_hashes(parse_query("tim rand")) > > p search_array_with_hashes(parse_query("crand")) > > # >> [2] > > # >> [1] > > # >> [3, 4] > > > Will that do the trick? > > Tim > > > On May 25, 1:23 am, Robert Scott <[email protected]> > > -- > Posted viahttp://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 [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---

