crashtech/torque-postgresql

Status of the record coder?

Opened this issue · 2 comments

I'm interested in implementing support for storing arbitrary record types in rows.

I saw that lib/torque/postgresql/coder.rb was removed in cf1c46f (message remove unnecessary code).

Do you recall whether it was working? If I get it running, would a PR be accepted?

Hey there. So that part of the code was removed because newer versions of the PG gem pretty much added all the support needed for the things the coder was doing. So, relying on the PG gem is better than using the previous coder.

BTW, I saw your PR and Composite type is something that I planned on adding to the gem, but using ActiveRecord's composed_of feature, which is quite unknown. So I will hold on that for now.

composed_of is for mapping multiple columns into a single struct, right? This maps a single column (containing a row value) into a struct, which is not quite the same thing (although I suppose you could use composed_of with a single argument).

This PR also supports instantiating instances of regular ActiveRecord::Base models, if they are stored in a column.

I intend to extend this to support associations - consider:

class Parent < ActiveRecord::Base
  has_many :children, class_name: "Child"
  def self.rolled_up(id)
    where(id: id).
    joins(:children).
    select("parents.*, array_agg(ROW(children.*)) as children").
    group(Parent.columns.map {|c| "#{Parent.table_name}.#{c.name}"})
  end
end
class Child < ActiveRecord::Base
  belongs_to :parent
end

This should preload the children relation with instances of Child.