Selecting Some Hashes From Set of Hashes Based On Value In Ruby -
overall problem i'm solving:
i'm pretty new ruby , working on creating search method app in user searches name of 1 model, foo, may have many instances of name, , return results of related instances of model, day. however, each foo has multiple versions of day associated (there 4 different versions total can associated with. display versions through foo, , foo contains them in booleans: include_in_standard, include_in_v1, etc.).
i need search method let me know versions foo included each day, can display search results accordingly. also, days available different kinds of users. i've gotten i'm passing information on days , versions out of model controller, however, in controller, i'm having problem getting days available current user.
the structure of code:
in model day, have search method returns following:
results={:result_case=>result_case, :days_set=>days_set, :term0=>term0, :term1=>term1}
result_case integer, term0 , term1 strings.
days_set set formed so
days_set=set.new array_of_foo.each |f| f.join_table_connecting_foo_and_days.days.each |foo| days_set+={:day=>d, :standard=>f.include_in_standard, :v1=>f.include_in_v1, :v2=>f.include_in_v2, :v3=>f.include_in_v3, :v4=>f.include_in_v4} end end days_set
in controller, call search method returns results, , assign local variable results in controller. have array of days accessible current_user, available_days.
the micro-goal:
i want set or array hashes days_set day included in available_days. is, if days_set currently
[{:day=>d1, :standard=>true, :v1=>false, :v2=>false, :v3=>true, :v4=>true}, {:day=>d2, :standard=>false, :v1=>true, :v2=>false, :v3=>true, :v4=>false}, {:day=>d3, :standard=>false, :v1=>false, :v2=>false, :v3=>true, :v4=>true}, {:day=>d4, :standard=>true, :v1=>true, :v2=>true, :v3=>true, :v4=>true}, {:day=>d5, :standard=>false, :v1=>false, :v2=>false, :v3=>true, :v4=>false}, {:day=>d6, :standard=>true, :v1=>false, :v2=>true, :v3=>false, :v4=>true}] available_days=[d1, d3, d4]
then want
[{:day=>d1, :standard=>true, :v1=>false, :v2=>false, :v3=>true, :v4=>true}, {:day=>d3, :standard=>false, :v1=>false, :v2=>false, :v3=>true, :v4=>true}, {:day=>d4, :standard=>true, :v1=>true, :v2=>true, :v3=>true, :v4=>true}]
the error/problem/code that's not working:
i've tried looking ideas on how elsewhere, don't think have coding vocabulary describe i'm trying do.
i thought work, generating can't convert symbol integer error on line marked ** (which not in code.)
days_set=results[:days_set] ** new_days_set=days_set.keep_if{|day_hash| available_days.include? day_hash[:day]
questions:
how can hashes in days_set day in available_days?
i've been running error, can't convert symbol integer, more other error, @ least once week, maybe more. feel indicates concept i'm not quite understanding. can tell me concept can information on and/or direct me information on it? don't know it's called i'm looking for, makes hard find it.
thank you.
to days subset:
new_days = days_set.select { |ds| available_days.include? ds[:day] }
this give array. or can use .to_set
on set:
new_days_set = (days_set.select { |ds| available_days.include? ds[:day] }).to_set
the "can't convert symbol integer" error occuring because somewhere you're trying compare symbol, such :day
integer value, such d1
. or if ds[:day]
(the value key :day
hash element ds
) symbol , available_days
array array of integers.
Comments
Post a Comment