I should say that the code I have works. I feel like there has to be a much more efficient way to do it (especially when I just crashed my server with it).Relevant schema:  create_table item_total_stats, force: :cascade do |t|    t.integer  item_id    t.integer  item_stat_type_id    t.integer  stat_amount    t.datetime created_at,        null: false    t.datetime updated_at,        null: false  end  create_table item_stat_types, force: :cascade do |t|    t.string   name    t.string   display_text    t.datetime created_at,   null: false    t.datetime updated_at,   null: false  end  create_table items, force: :cascade do |t|    t.string   display_name    t.datetime created_at,   null: false    t.datetime updated_at,   null: false  endRelevant models:  class ItemStatType < ActiveRecord::Base    has_many :item_stats    has_many :item_total_statsendclass ItemTotalStat < ActiveRecord::Base  belongs_to :item  belongs_to :item_stat_typeendclass Item < ActiveRecord::Base  has_many :item_total_statsendOverview:There are approximately 30 different ItemStatTypes. I am trying to setup a filter with boxes to allow users to enter a minimum and a maximum for each one. At the moment, all ~30 ItemStatTypes are hard coded as a scope in the Item model.An example of the 2 scopes for each ItemStatType:  scope :saim_g, lambda{ |reference_value|    found_stats = [ItemStatType.where(name: Aim).first.id]    # get a reference to the join table    total_stats = ItemTotalStat.arel_table    # get a reference to the filtered table    items = Item.arel_table    # let AREL generate a complex SQL query    puts reference_value    where(      ItemTotalStat \        .where(total_stats[:item_id].eq(items[:id])) \        .where(total_stats[:item_stat_type_id].in([*found_stats].map(&:to_i))) \        .where(stat_amount >= ?, reference_value) \        #.where(total_stats[:stat_amount].eq(reference_value)) \        .exists    )  }  scope :saim_l, lambda{ |reference_value|    found_stats = [ItemStatType.where(name: Aim).first.id]    # get a reference to the join table    total_stats = ItemTotalStat.arel_table    # get a reference to the filtered table    items = Item.arel_table    # let AREL generate a complex SQL query    puts reference_value    where(      ItemTotalStat \        .where(total_stats[:item_id].eq(items[:id])) \        .where(total_stats[:item_stat_type_id].in([*found_stats].map(&:to_i))) \        .where(stat_amount <= ?, reference_value) \        #.where(total_stats[:stat_amount].eq(reference_value)) \        .exists    )  }I am creating the view like this:<% ItemStatType.order('name asc').each do |stat_type| %>    <div class=swtor-ui style=float: left;position: relative; width: 250px;>        <%= stat_type.name %><%= render_filterrific_spinner %><br>        <% begin %>            <% greater_then_string = s#{stat_type.name.downcase}_g %>            <% less_then_string = s#{stat_type.name.downcase}_l %>            <%= f.text_field(                greater_then_string.parameterize.underscore.to_sym,                placeholder: minumum,                class: filterrific-periodically-observed            ) %>            --            <%= f.text_field(                less_then_string.parameterize.underscore.to_sym,                placeholder: maximum,                class: filterrific-periodically-observed            ) %>        <% rescue %>            Unknown error occured        <% end %>    </div><% end %>Basically, I am trying to figure out a better way to go about this. Since it can run through the scopes multiple times it takes a good amount of processing power (and crashed my server while it was hidden behind a beta tag and only about 3 people had access to it).When I was originally trying to build this, I was hoping to originally find a way to have this all happen in one scope. But without hard coding (which I ended up doing anyways) each ItemStatType, I could not find a way to do that with having the form the way I want it to look.
Multiple many-to-many filter with range using filteriffic
performance;ruby;ruby on rails