1

ActiveRecord in Rails 3 or Padrino is ignoring GROUP_CONCAT inside a .select.

I'm trying to figure out why AcitveRecord is ignoring this query:

Dvd.includes(:dvd_director, :dvd_producer).
      select(" 
      GROUP_CONCAT(DISTINCT dvd_director.director SEPARATOR ', ') AS director
      , GROUP_CONCAT(DISTINCT dvd_producer.producer SEPARATOR ', ') AS producer
      ...
      .where("id = 4")

The query gets executed but all the GROUP_CONCATs get ignored and the info is not in the result. This is a simpliefied version of this question which hasn't been answered.

I read somewhere that you should use .cacluate for this, but that gives me an error.

What gives?

Community
  • 1
  • 1
kakubei
  • 5,321
  • 4
  • 44
  • 66

2 Answers2

0

RAILS 4

I had a similar problem with my query:

@project
.fields_numbers_projects
.select("*, GROUP_CONCAT(fields_numbers_projects.dates_project_id) AS dpid")
.group(:group_id)
.each do |fpn|

And the result of ActiveRecord was:

<#FieldsNumbersProject 
id: 1, group_id: 1, project_id: 1, dates_project_id: 1, fields_project_id: 1, employee_id: nil>
<#FieldsNumbersProject 
id: 1, group_id: 2, project_id: 1, dates_project_id: 1, fields_project_id: 1, employee_id: nil>

It had no sign of "dpid" or just GROUP_CONCAT(dates_project_id) in the ActiveRecord result whereas you could use it as fpn.dpid.

I got fooled for ten minutes by the ActiveRecord result without trying to print.

Nynevi
  • 1
  • 4
  • I think I later read that this wasn't supported because AR has no way of knowing which DB technology you're using, so some features are unavailable. – kakubei Aug 30 '15 at 08:28
0

I think I later read that this wasn't supported because AR has no way of knowing which DB technology you're using, so some features are unavailable, and apparently GROUP_CONCAT is not available to every SQL tech out there.

kakubei
  • 5,321
  • 4
  • 44
  • 66