Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to dynamically construct attribute names in Sunspot searchable block
    text
    copied!<p>My search includes the ability to scope a query by a price range, the day of the week, and a guest count. </p> <p>However, the price is different on each day of the week, and the price changes (not linearly) by the number of guests. The best way I can think of to index the data is to create a different field for each day/guest combo, e.g.,</p> <pre><code>searchable do integer :price_sunday_2_guests do price(:sunday, 2) end integer :price_sunday_3_guests do price(:sunday, 3) end ... integer :price_monday_2_guests do price(:monday, 2) end ... # and so on... end </code></pre> <p>Obviously, I don't want to type all that in. I want to construct those attributes in the searchable blocks. More like:</p> <pre><code>searchable do Date::DAYNAMES.each do |day_name| day = DateTime.strptime(day_name, '%A') (guests_min..guests_max).each do |guests| sym = (day_name.downcase + '_' + guests.to_s + 'guests_price_abs_max').to_sym integer sym do price(day_name, guests) end end end end </code></pre> <p>But I get the following exception: NoMethodError: undefined method `guests_min' for #&lt;Sunspot::DSL::Fields:0x000001080bdcf0&gt;</p> <p>It does not complain about the constant Date::DAYNAMES.</p> <p>It seems that Sunspot tries to interpret any method token as a field. Which, I suppose would even make sense if I understood it better. </p> <p>So, my question, is there a smart way for me to do this? Do I need to just hardcode a range and let my price method return an empty value? Is there some mechanism available in the searchable block I'm not aware of?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload