From e8131f360879c7a6cd052e436742d08d8454167d Mon Sep 17 00:00:00 2001 From: Matchu Date: Wed, 2 Aug 2023 16:29:31 -0700 Subject: [PATCH] Fix bug with Arel ordering Ok so weird little situation, usually Arel will accept an attribute as a param to `order()`, but not when it's in a very specific situation of all of the following: `Item.joins(:translations).includes(:translations).limit(30).order(Item::Translation.arel_table[:name])` For some reason, it's all like "hey I can't call `to_sql` on an attribute!", but only in the scenario where all 3 of those other things are present. Weird! Anyway, explicitly saying `.asc` fixes this. Ok! --- app/models/closet_hanger.rb | 6 +++--- app/models/color.rb | 5 ++++- app/models/item.rb | 2 +- app/models/species.rb | 5 ++++- app/models/zone.rb | 5 +++-- 5 files changed, 15 insertions(+), 8 deletions(-) diff --git a/app/models/closet_hanger.rb b/app/models/closet_hanger.rb index 7e2b4734..5a759fc8 100644 --- a/app/models/closet_hanger.rb +++ b/app/models/closet_hanger.rb @@ -12,9 +12,9 @@ class ClosetHanger < ApplicationRecord validate :list_belongs_to_user scope :alphabetical_by_item_name, -> { - joins(:item => :translations). - where(Item::Translation.arel_table[:locale].eq(I18n.locale)). - order(Item::Translation.arel_table[:name]) + it = Item::Translation.arel_table + joins(:item => :translations).where(it[:locale].eq(I18n.locale)). + order(it[:name].asc) } scope :newest, -> { order(arel_table[:created_at].desc) } scope :owned_before_wanted, -> { order(arel_table[:owned].desc) } diff --git a/app/models/color.rb b/app/models/color.rb index dc269d06..50b90822 100644 --- a/app/models/color.rb +++ b/app/models/color.rb @@ -1,7 +1,10 @@ class Color < ApplicationRecord translates :name - scope :alphabetical, -> { with_translations(I18n.locale).order(Color::Translation.arel_table[:name]) } + scope :alphabetical, -> { + ct = Color::Translation.arel_table + with_translations(I18n.locale).order(ct[:name].asc) + } scope :basic, -> { where(:basic => true) } scope :standard, -> { where(:standard => true) } scope :nonstandard, -> { where(:standard => false) } diff --git a/app/models/item.rb b/app/models/item.rb index 452788aa..e8e1879f 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -27,7 +27,7 @@ class Item < ApplicationRecord locale = locale or I18n.locale it = Item::Translation.arel_table joins(:translations).where(it[:locale].eq('en')). - order(it[:name]) + order(it[:name].asc) } scope :newest, -> { diff --git a/app/models/species.rb b/app/models/species.rb index 29e46311..8083dd22 100644 --- a/app/models/species.rb +++ b/app/models/species.rb @@ -1,7 +1,10 @@ class Species < ApplicationRecord translates :name - scope :alphabetical, -> { with_translations(I18n.locale).order(Species::Translation.arel_table[:name]) } + scope :alphabetical, -> { + st = Species::Translation.arel_table + with_translations(I18n.locale).order(st[:name].asc) + } scope :matching_name, ->(name, locale = I18n.locale) { st = Species::Translation.arel_table diff --git a/app/models/zone.rb b/app/models/zone.rb index 8ddc10c5..887fba03 100644 --- a/app/models/zone.rb +++ b/app/models/zone.rb @@ -1,4 +1,4 @@ -class Zone < ApplicationRecord +class Zone < ActiveRecord::Base translates :label, :plain_label # When selecting zones that an asset occupies, we allow the zone to set @@ -6,7 +6,8 @@ class Zone < ApplicationRecord attr_writer :sometimes scope :alphabetical, -> { - with_translations(I18n.locale).order(Zone::Translation.arel_table[:label]) + zt = Zone::Translation.arel_table + with_translations(I18n.locale).order(zt[:label].asc) } scope :includes_translations, -> { includes(:translations) } scope :matching_label, ->(label, locale = I18n.locale) {