jan javinto
2014-Nov-13 13:32 UTC
ActiveRecord#minimum generates different column names with group by
Hi, With Rails 4.1.7 I want to build a generic query calculating the minimum of a 'position' field. The 'group by' of this query is dynamic. There may be one or not, like so: grouping=[] Item.group(grouping).having("minimum_position>1").minimum(:position).each do |item, min_pos| .... end I found out that if there is a grouping Item.group(:parent).having("minimum_position>1").minimum(:position) will generate: SELECT *MIN(`items`.`position`) AS minimum_position*, parent_type AS parent_type, parent_id AS parent_id, position AS position FROM `items` GROUP BY parent_type, parent_id, position HAVING minimum_position>1 And if there is no grouping: Item.having("minimum_position>1").minimum(:position) will generate: SELECT *MIN(`items`.`position`) AS minimum_id* FROM `items` HAVING minimum_position>1 which leads to an exception error as there is no field 'minimum_position' Why do these two queries do not generate the same column name for the Minimum(:field) function? Would it not be more Rails like to generate the same column name? Would it be worth proposing it as an issue? Thanks Jan -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-core+unsubscribe@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core. For more options, visit https://groups.google.com/d/optout.