Is there a better way to write this ActiveRecord Rails 4.2 query using scopes?
<p>So I'm building a UI to let a user find a zoo by animal and/or by trainer for a Rails 4 application with postgres.</p>

<p>There are a series of animal checkboxes and a series of trainer checkboxes. A user may check multiple checkboxes in both the trainer and animal sections.</p>

<p><em>here is the rails code I am using to build the query</em></p>

<pre><code>animal_ids = [1, 2, 3]
trainer_ids = [1, 2] 

# OR query - either an animal or trainer can be present to find a zoo
a = Zoo.includes(:animals).where(animals: { id: animal_ids}) 
b = Zoo.includes(:trainers).where(trainers: { id: trainer_ids})
zoos = a+b
</code></pre>

<p>This results in 4 sql queries in the console.  Is there a more efficient way to write this?  Should I use raw sql with an OR condition?</p>

<p><strong>model setup</strong></p>

<pre><code>class Zoo &lt; ActiveRecord::Base 
  has_many :animals, through: zoo_animals
  has_many :trainers, through: zoo_trainers
  has_many :zoo_trainers
  has_many :zoo_animals
end  

class Animal &lt; ActiveRecord::Base
  has_many :zoos, through :zoo_animals
  has_many :zoo_animals
end  

class Trainer &lt; ActiveRecord::Base
  has_many :zoos, through :zoo_trainers 
  has_many :zoo_trainers
end  

class ZooAnimal &lt; ActiveRecord::Base
  belongs_to :animal
  belongs_to :zoo
end

class ZooTrainer &lt; ActiveRecord::Base
  belongs_to :zoo
  belongs_to :trainer
end
</code></pre>