Hanami repository delete/update operations using joins

224 Views Asked by At

I'm having some trouble doing delete/update operations using joins on a repository. I have Libraries that belong to Users and have many Books, like so:

class LibraryRepository < Hanami::Repository
  associations do
    belongs_to: user
    has_many :books
  end

class BookRepository < Hanami::Repository
  associations do
    belongs_to :library
  end

Now what I want to delete a book entry, but only if it belongs to the user library. I was trying to do that using the following query:

books.join(libraries).where(libraries[:user_id] => user_id).where(id: id).delete

But I get the following error:

Sequel::Error: Need multiple FROM tables if updating/deleting a dataset with JOINs

Any suggestions into how I could do such a query?

1

There are 1 best solutions below

1
On

I think Hanami do not support multiple from, but postgresql does

books.dataset.from(:books, :libraries).where(Sequel[:libraries][:user_id] => user_id, Sequel[:id] => id).delete