forked from fixin.me/fixin.me
Fix SQLite query compatibility inside WITH clause
SQLite3::SQLException: circular reference
This commit is contained in:
@@ -77,15 +77,13 @@ class Unit < ApplicationRecord
|
|||||||
.from(units).group(:base_id, :symbol)
|
.from(units).group(:base_id, :symbol)
|
||||||
}
|
}
|
||||||
scope :ordered, ->{
|
scope :ordered, ->{
|
||||||
left_outer_joins(:base).order(ordering)
|
left_outer_joins(:base).order([
|
||||||
}
|
arel_table.coalesce(Arel::Table.new(:bases_units)[:symbol], arel_table[:symbol]),
|
||||||
|
|
||||||
def self.ordering
|
|
||||||
[arel_table.coalesce(Arel::Table.new(:bases_units)[:symbol], arel_table[:symbol]),
|
|
||||||
arel_table[:base_id].not_eq(nil),
|
arel_table[:base_id].not_eq(nil),
|
||||||
:multiplier,
|
arel_table[:multiplier],
|
||||||
:symbol]
|
arel_table[:symbol]
|
||||||
end
|
])
|
||||||
|
}
|
||||||
|
|
||||||
before_destroy do
|
before_destroy do
|
||||||
# TODO: disallow destruction if any object depends on this unit
|
# TODO: disallow destruction if any object depends on this unit
|
||||||
@@ -114,11 +112,10 @@ class Unit < ApplicationRecord
|
|||||||
|
|
||||||
def successive
|
def successive
|
||||||
units = Unit.arel_table
|
units = Unit.arel_table
|
||||||
lead = Arel::Nodes::NamedFunction.new('LAG', [units[:id]])
|
Unit.with(units_with_lag: user.units.ordered.select(
|
||||||
window = Arel::Nodes::Window.new.order(*Unit.ordering)
|
units[Arel.star],
|
||||||
lag_id = lead.over(window).as('lag_id')
|
Arel::Nodes::NamedFunction.new('LAG', [units[:id]]).over.as('lag_id')
|
||||||
Unit.with(
|
)).from(Arel::Table.new(:units_with_lag).as(:units))
|
||||||
units: user.units.left_outer_joins(:base).select(units[Arel.star], lag_id)
|
.where(units[:lag_id].eq(id)).first
|
||||||
).where(units[:lag_id].eq(id)).first
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user