Right now I have this query:
@events = Event.joins(:packages).where("packages.kind IN (?)", packages).distinct
And it returns objects that match just a single attribute in the packages
array.
I would like ActiveRecord to only return objects that match all attributes in the given array. How would that be done with my query set up the way it is?
I feel like there must be a simpler way of doing this. But, in Postgres, try:
Event.joins(:packages).having('array_agg(packages.type) @> array[?]', packages).group(:id)
You can construct a query with multiple conditions
@events = Event.joins(:packages).distinct
packages.each do |package|
@events = @events.where("packages.kind = ?", package)
end
@events
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With