torque-postgresql icon indicating copy to clipboard operation
torque-postgresql copied to clipboard

Status of the record coder?

Open DanielHeath opened this issue 2 years ago • 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 cf1c46fb70fe1300e4f97d5e9f07ee6329321113 (message remove unnecessary code).

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

DanielHeath avatar Nov 08 '22 11:11 DanielHeath

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.

crashtech avatar Nov 09 '22 18:11 crashtech

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.

DanielHeath avatar Nov 13 '22 23:11 DanielHeath