diff --git a/lib/jsonapi/resource.rb b/lib/jsonapi/resource.rb index 6fa78dff..95ebf6a6 100644 --- a/lib/jsonapi/resource.rb +++ b/lib/jsonapi/resource.rb @@ -735,7 +735,8 @@ def _build_joins(associations) joins = [] associations.inject do |prev, current| - joins << "LEFT JOIN #{current.table_name} AS #{current.name}_sorting ON #{current.name}_sorting.id = #{prev.table_name}.#{current.foreign_key}" + relationship_primary_key = current.options.fetch(:primary_key, "id") + joins << "LEFT JOIN #{current.table_name} AS #{current.name}_sorting ON #{current.name}_sorting.#{relationship_primary_key} = #{prev.table_name}.#{current.foreign_key}" current end joins.join("\n")