Relation's dataset doesn't apply when the relation is used in a join

Open
#297 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
25/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
ruby, sql
Domain
backend, databases

Research direction

Start with the UserRepo#with_undone_tasks entry point and the relation definitions for UndoneTasks and Users. Trace how join(undone_tasks) and combine(:undone_tasks) build the SQL query, checking where the undone_tasks dataset filters and ordering are lost. Done means the generated join includes the dataset conditions and ordering while loading the combined undone tasks.

Written by the indexing model from the issue text.

Description

Hi! Here I make use of relation dataset to model different types of a task. In UserRepo I want a fetch all users having undone tasks and I want to load them with their undone tasks. The issue is that join with undone_tasks relation doesn't build a SQL query with the filters from undone_tasks dataset.

class Users < ROM::Relation[:sql]
  schema(infer: true) do
    associations do
      has_many :done_tasks
      has_many :undone_tasks
      has_many :tasks
    end
  end
end

class Tasks < ROM::Relation[:sql]
  schema(:tasks, infer: true) do
    associations { belongs_to :user }
  end
end

class UndoneTasks < ROM::Relation[:sql]
  schema(:tasks, infer: true) do
    associations { belongs_to :user }
  end

  dataset { where(done: false).order(:created_at).qualify }
end

class DoneTasks < ROM::Relation[:sql]
  schema(infer: true) do
    associations { belongs_to :user }
  end

  dataset { where(done: true).order(:created_at).qualify }
end


class UserRepo < ROM::Repository::Root[:users]
  def with_undone_tasks
    users
        .join(undone_tasks)
        .combine(:undone_tasks)
        .to_a
  end
end
Dominant language
Ruby
Stars
220
Forks
97
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from rom-rb/rom-sql

All issues in rom-rb/rom-sql

Similar issues

More Ruby issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.