Вопрос

Overall Problem I'm Solving:

I'm pretty new to Ruby and working on creating a search method for my app in which the user searches for the name of one model, foo, which may have many instances of that name, and we return the results of all the related instances of another model, Day. However, each foo has multiple versions of day it is associated with (there are 4 different versions total it can be associated with. We display those versions through foo, and foo contains them in booleans: include_in_standard, include_in_v1, etc.).

I need my search method to let me know which versions foo is included for each day, so that I can display the search results accordingly. Also, only certain days are available to different kinds of users. I've gotten it so that I'm passing the information on the days and versions out of the model to the controller, however, in the controller, I'm having a problem getting only the days which are available to the current user.

The Structure Of The Code:

In the model for Day, I have a search method which returns the following:

results={:result_case=>result_case, :days_set=>days_set, :term0=>term0, :term1=>term1}

result_case is an integer, term0 and term1 are strings.

days_set is a Set which is formed like so

days_set=Set.new
array_of_foo.each do |f|
  f.join_table_connecting_foo_and_days.days.each do |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 my controller, I call the search method which returns results, and assign it to the local variable results in the controller. I also have an array of all the days which are accessible to the current_user, available_days.

The Micro-Goal:

I want to get a set or array which is just the hashes from days_set where the day is included in available_days. That is, if my days_set is 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 I 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 for ideas on how to do this elsewhere, but I don't think I have the coding vocabulary to describe what I'm trying to do.

I thought this would work, but it is generating a Can't convert Symbol to Integer error on the line marked ** (which is not in the code.)

days_set=results[:days_set]
** new_days_set=days_set.keep_if{|day_hash| available_days.include? day_hash[:day]

Questions:

  1. How can I get the hashes in my days_set whose day is in available_days?

  2. I've been running into this error, Can't convert Symbol to Integer, more than any other error, at least once a week, maybe more. I feel like this indicates some concept I'm not quite understanding. Can you tell me what that concept is so I can look for information on it and/or direct me to some information on it? I don't know what it's called that I'm looking for, which makes it hard to find it.

Thank you.

Это было полезно?

Решение

To get your days subset:

new_days = days_set.select { |ds| available_days.include? ds[:day] }

This will give you an array. Or you can use .to_set on it to get a set:

new_days_set = (days_set.select { |ds| available_days.include? ds[:day] }).to_set

The "can't convert symbol to integer" error is occuring because somewhere you're probably trying to compare a symbol, such as :day to an integer value, such as d1. Or if ds[:day] (the value for key :day for hash element ds) is a symbol and your available_days array is an array of integers.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top