Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
drqCode opened this issue Jul 13, 2018 · 2 comments
Open

Comments

@drqCode
Copy link

drqCode commented Jul 13, 2018

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
@wmaciejak
Copy link
Contributor

Try to define keys matching, something like join(undone_tasks, id: :user_id)
I had similar issue a few months ago.

@solenko
Copy link

solenko commented Dec 20, 2021

Have the same issue with joins.
Кузкщвгсфиду example can be minimised to:

class Users < ROM::Relation[:sql]
  schema(infer: true) do
    associations do
      has_many :undone_tasks
    end
  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

users.join(undone_tasks) # dataset condition is ignored

Is there any workaround or recommended approach on how to handle such use-cases?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants