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!
This commit is contained in:
Emi Matchu 2023-08-02 16:29:31 -07:00
parent c2866f832d
commit 111d4dcaee
5 changed files with 15 additions and 8 deletions

View file

@ -12,9 +12,9 @@ class ClosetHanger < ApplicationRecord
validate :list_belongs_to_user validate :list_belongs_to_user
scope :alphabetical_by_item_name, -> { scope :alphabetical_by_item_name, -> {
joins(:item => :translations). it = Item::Translation.arel_table
where(Item::Translation.arel_table[:locale].eq(I18n.locale)). joins(:item => :translations).where(it[:locale].eq(I18n.locale)).
order(Item::Translation.arel_table[:name]) order(it[:name].asc)
} }
scope :newest, -> { order(arel_table[:created_at].desc) } scope :newest, -> { order(arel_table[:created_at].desc) }
scope :owned_before_wanted, -> { order(arel_table[:owned].desc) } scope :owned_before_wanted, -> { order(arel_table[:owned].desc) }

View file

@ -1,7 +1,10 @@
class Color < ApplicationRecord class Color < ApplicationRecord
translates :name 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 :basic, -> { where(:basic => true) }
scope :standard, -> { where(:standard => true) } scope :standard, -> { where(:standard => true) }
scope :nonstandard, -> { where(:standard => false) } scope :nonstandard, -> { where(:standard => false) }

View file

@ -27,7 +27,7 @@ class Item < ApplicationRecord
locale = locale or I18n.locale locale = locale or I18n.locale
it = Item::Translation.arel_table it = Item::Translation.arel_table
joins(:translations).where(it[:locale].eq('en')). joins(:translations).where(it[:locale].eq('en')).
order(it[:name]) order(it[:name].asc)
} }
scope :newest, -> { scope :newest, -> {

View file

@ -1,7 +1,10 @@
class Species < ApplicationRecord class Species < ApplicationRecord
translates :name 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) { scope :matching_name, ->(name, locale = I18n.locale) {
st = Species::Translation.arel_table st = Species::Translation.arel_table

View file

@ -1,4 +1,4 @@
class Zone < ApplicationRecord class Zone < ActiveRecord::Base
translates :label, :plain_label translates :label, :plain_label
# When selecting zones that an asset occupies, we allow the zone to set # When selecting zones that an asset occupies, we allow the zone to set
@ -6,7 +6,8 @@ class Zone < ApplicationRecord
attr_writer :sometimes attr_writer :sometimes
scope :alphabetical, -> { 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 :includes_translations, -> { includes(:translations) }
scope :matching_label, ->(label, locale = I18n.locale) { scope :matching_label, ->(label, locale = I18n.locale) {