ruby on rails - How do you search through a hash mapped from an ActiveRecord result? -


i have hash variable contains results activerecord search, iterated through display data. code (transcribed) goes somewhere this:

 hvar=user.map{|x| {:name => x.name, :type => x.type, :section => x.section,         :result => (x.var1/x.var2).round(1)}} 

the hash variable 'hvar' display following through .inspect :

 [     {:name=>'michael', :type=>7, :section=>1, :result=>4.1},     {:name=>'seymour', :type=>4, :section=>1, :result=>3.9},     {:name=>'walter', :type=>2, :section=>1, :result=>6.3},     {:name=>'josephine', :type=>7, :section=>1, :result=>5.4},     {:name=>'carla', :type=>7, :section=>0, :result=>5.4}  ] 

so far, good.

now, wish search through resulting hash, e.g. records of type 7, , i'm not sure how it. found this:

mission=hvar.select{|k| k[:type] == 7} 

but gives me 0 results, makes sense me, think searching through "first level" of hash (i.e. 0, 1, 2, 3) instead of subhashes within.

how find records type 7? , effect, how search on 2 fields? say, type == 7 , section == 1.

in case you're wondering, i'm not doing search activerecord itself, cause have iterate through every single record , arrange them in pivoted table merges data table. so, make more efficient figured use hash instead of iterating through activerecord, it's spitting somewhere around 1700 sql queries.

thanks in advance.

your code written correctly. thing can answered is

how search on 2 fields? say, type == 7 , section == 1.

the same way you're doing now, plus additional condition:

mission = hvar.select { |k| k[:type] == 7 && k[:section] == 1} 

Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -