Skip to content

Commit

Permalink
fix relation computed attribute primary key must be optional and id b…
Browse files Browse the repository at this point in the history
…y default
  • Loading branch information
diegosenarruzza committed Oct 28, 2020
1 parent e36e67e commit 63dd8b4
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/rasti/db/computed_attributes/relation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 63dd8b4

Please sign in to comment.