How to form a search predicate with one to many model

43 Views Asked by At

I have a Company and a Certificate model. Each Company can have many Certificates. There is a one to many relationship.

I cannot figure out how to form a predicate that will fetch all the certificates of a set of Companies.

Here is a bare bones view:

class Company {
  @Relationship(deleteRule: .cascade, inverse: \Certificate.company)
  var certs: [Certificate]?
}

class Certificate {
  var company: Company
}

import SwiftUI
import SwiftData

struct MyView: View {

  @Query var certs: [Certificate]

  init(appContext: AppContext) {
    let companies = multiSel // multiSel is a Set<Company>
    
    let predicate = #Predicate<Certificate> {
      companies.contains($0.company)
    }
    _certs = Query(filter: predicate, animation: .snappy)
  }
  
}

The code above results the following error:

Static method 'build_contains' requires that 'Company' conform to 'Collection'

0

There are 0 best solutions below