Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Bug in select_map with aliased field? #599

Closed
kbd opened this Issue · 1 comment

2 participants

@kbd
kbd commented

This:

Entry.group_and_count{DATE(creation_datetime).as(days)}.select_map(:days)

doesn't work, while:

d = Proc.new{DATE(creation_datetime).as(days)}
Entry.group_and_count(&d).select_map(&d)

does. Is this expected?

FWIW, here's the intermediate SQL for the query before the select_map call:

irb(main):018:0> Entry.group_and_count{DATE(creation_datetime).as(days)}
=> #<Sequel::Mysql2::Dataset: "SELECT DATE(`creation_datetime`) AS `days`, count(*) AS `count` FROM `entries` GROUP BY DATE(`creation_datetime`) ORDER BY `creation_datetime`">

As you can see, the field is being appropriately aliased, so I don't know why select_map isn't able to reference the alias.

@jeremyevans
Owner

This isn't a bug. select_map(:days) is basically select(:days).map(:days), and the select(:days) call overrides the SELECT clause added by group_and_count. If you used an SQL logger, this would probably be more obvious. The reason your &d example works is that you are using the same argument in both calls, while in your first example the argument given to group_and_count is different than the argument given to select_map.

Long story short, use map(:days) instead of select_map(:days) in cases where you are setting the SELECT clause via some other method.

@jeremyevans jeremyevans closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.