lib/active_median/model.rb in active_median-0.3.0 vs lib/active_median/model.rb in active_median-0.3.1
- old
+ new
@@ -37,10 +37,13 @@
# prevent SQL injection
percentile = connection.quote(percentile)
group_values = all.group_values
+ # replace select to match behavior of average
+ relation = unscope(:select)
+
relation =
case connection.adapter_name
when /mysql/i
# assume mariadb by default
# use send as this method is private in Rails 4.2
@@ -49,24 +52,24 @@
if mariadb
if group_values.any?
over = "PARTITION BY #{group_values.join(", ")}"
end
- select(*group_values, "PERCENTILE_CONT(#{percentile}) WITHIN GROUP (ORDER BY #{column}) OVER (#{over}) AS #{column_alias}").unscope(:group)
+ relation.select(*group_values, "PERCENTILE_CONT(#{percentile}) WITHIN GROUP (ORDER BY #{column}) OVER (#{over}) AS #{column_alias}").unscope(:group)
else
# if mysql gets native function, check (and memoize) version first
- select(*group_values, "PERCENTILE_CONT(#{column}, #{percentile}) AS #{column_alias}")
+ relation.select(*group_values, "PERCENTILE_CONT(#{column}, #{percentile}) AS #{column_alias}")
end
when /sqlserver/i
if group_values.any?
over = "PARTITION BY #{group_values.join(", ")}"
end
- select(*group_values, "PERCENTILE_CONT(#{percentile}) WITHIN GROUP (ORDER BY #{column}) OVER (#{over}) AS #{column_alias}").unscope(:group)
+ relation.select(*group_values, "PERCENTILE_CONT(#{percentile}) WITHIN GROUP (ORDER BY #{column}) OVER (#{over}) AS #{column_alias}").unscope(:group)
when /sqlite/i
- select(*group_values, "PERCENTILE(#{column}, #{percentile} * 100) AS #{column_alias}")
+ relation.select(*group_values, "PERCENTILE(#{column}, #{percentile} * 100) AS #{column_alias}")
when /postg/i, /redshift/i # postgis too
- select(*group_values, "PERCENTILE_CONT(#{percentile}) WITHIN GROUP (ORDER BY #{column}) AS #{column_alias}")
+ relation.select(*group_values, "PERCENTILE_CONT(#{percentile}) WITHIN GROUP (ORDER BY #{column}) AS #{column_alias}")
else
raise "Connection adapter not supported: #{connection.adapter_name}"
end
# same as average