Skip to content

Filtering a HasManyThrough relationship on the intermediary table #1333

Answered by groue
tcwalther asked this question in Q&A
Discussion options

You must be logged in to vote

Hello @tcwalther,

You know that associations, like requests, can be filtered:

let request = countryRequest
    .filter(...) // filtered request
    .including(all: Country.citizens.filter(...)) // filtered association

Your solution is thus to build the HasManyThrough association from a filtered intermediary association:

extension Country {
    static let validPassports = passports.filter(Column("isValid"))
    static let validCitizens = hasMany(Citizen.self, through: validPassports, using: Passport.citizen)
}

let request = countryRequest.including(all: Country.validCitizens)

Also possible:

extension Country {
    static func passports(valid: Bool) -> HasManyAssociation<Self, Passport> {

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@tcwalther
Comment options

@groue
Comment options

Answer selected by tcwalther
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants