How to query on a hasAndBelongsToMany relation
mattfeury opened this issue · comments
Hello, apologies if this is the wrong place for this question. I'm wondering how I can query based on a hasAndBelongsToMany relation.
for example, say i have these classes:
object Plan extends ActiveRecordCompanion[Plan]
case class Plan(active:Boolean = true) extends ActiveRecord with Timestamps {
lazy val events = hasAndBelongsToMany[Event]
}
object Event extends ActiveRecordCompanion[Event]
case class Event(name:String) extends ActiveRecord with Timestamps {
lazy val plans = hasAndBelongsToMany[Plan]
}
how can I query for Events with a given Plan? i.e. this:
def getEventsForPlan(plan:Plan) : List[Event] = {
Event.includes(_.plans).where { event =>
(event.active === true) and
(plan in event.plans) // this won't work. how can i do this?
}.toList
}
that gives me value in is not a member of com.github.aselab.activerecord.ActiveRecord.BelongsToAssociation[subscription.type,models.Plan]
any ideas?
think i figured this out. eventually i'll stop posting questions that i answer myself :)
it seems to work by making the query off the plan object itself
def getEventsForPlan(plan:Plan) : List[Event] = {
plan.events.includes(_.plans).where { event =>
(event.active === true)
}.toList
}