Skip to content

Wrong code produced, syntax error, too #4

Open
@camertron

Description

@camertron

(Originally filed by @martinstreicher against arel-helpers)

My solution for this SQL:

SELECT weights.* 
FROM weights 
WHERE captured_at IN (
SELECT MIN(captured_at) 
FROM weights 
GROUP BY DATE(captured_at))

Was (in my code)…

class Weight < ActiveRecord::Base
  def self.by_created_at_asc
    order created_at: :asc
  end

  def self.by_captured_at_asc
    order captured_at: :asc
  end

  def self.earliest_captured_at
    table
      .project(table[:captured_at].minimum)
      .group(Arel::Nodes::NamedFunction.new('DATE', [table[:captured_at]]))
  end

  def self.first_of_each_day
    where(table[:captured_at].in(earliest_captured_at)).by_captured_at_asc
  end

  def self.table
    self.arel_table
  end
end

Scuttle had emitted this:

Weight.select(Weight.arel_table[Arel.star]).where(
  Weight.arel_table[:captured_at].in(
    Weight.select(Weight.arel_table[:captured_at].minimum).group(
      Arel::Nodes::NamedFunction.new('DATE', [:captured_at])
    ).ast
  )
)

The syntax for NamedFunction is wrong, for one. And the .ast should no longer be passed in. I traced thru Arel to debug this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions