3

I need to do some absolute values in ARel. Basically need to understand how to execute this line (in Relational Algebra) using ARel:

Assume P is a table with columns value1 and value2:

Project((|p.value1 - 10| + |p.value2 - 10|) as match) P

Not sure if this is possible with ARel yet.

Thanks in advance!

Nima Gardideh
  • 575
  • 1
  • 4
  • 10
  • I think you are stuck with the num.abs method for now: http://corelib.rubyonrails.org/classes/Numeric.html#M001355 – ScottJShea Mar 13 '12 at 20:09
  • Yeah, I'm thinking it's basically that for now. I've ended up using ActiveRecord with order and using ABS from sqlite. It's working for now. – Nima Gardideh Mar 13 '12 at 20:38

1 Answers1

1

You're better off doing this is SQL:

class Project < ActiveRecord::Base
  def self.abs_values
    connection.select_values('SELECT abs(value1-10), abs(value2-10) FROM projects')
  end
end
Winfield
  • 18,985
  • 3
  • 52
  • 65