From 63dd8b4026c62361912ee85ceac7d8ba8aedd716 Mon Sep 17 00:00:00 2001 From: dsenarruzza Date: Wed, 28 Oct 2020 15:48:52 -0300 Subject: [PATCH] fix relation computed attribute primary key must be optional and id by default --- lib/rasti/db/computed_attributes/relation.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/rasti/db/computed_attributes/relation.rb b/lib/rasti/db/computed_attributes/relation.rb index 73f3786..fe0a470 100644 --- a/lib/rasti/db/computed_attributes/relation.rb +++ b/lib/rasti/db/computed_attributes/relation.rb @@ -3,21 +3,22 @@ module DB module ComputedAttributes class Relation - def initialize(value:, table:, type:, foreign_key:, attributes: []) + def initialize(value:, table:, type:, foreign_key:, primary_key: :id, attributes: []) @value = value @table = table @type = type - @attributes = attributes.push foreign_key @foreign_key = foreign_key + @primary_key = primary_key + @attributes = attributes.push foreign_key end def apply_to(dataset, name) - dataset.join_table(type, query_to(name), foreign_key => :id) + dataset.join_table(type, query_to(name), foreign_key => primary_key) end private - attr_reader :value, :table, :type, :attributes, :foreign_key + attr_reader :value, :table, :type, :foreign_key, :primary_key, :attributes def query_to(name) table.select{ |v| [value.as(:value), *attributes] }.group(foreign_key).as(name)