I would split it to two scopes
```ruby
def self.in_environments(environments)
  joins(:environment_product).where(:environment_products => { :environment_id => environments })
end

def self.in_products(products)
  joins(:environment_product).where(:environment_products => { :product_id => products })
end
```
I think it should work with both array of ids or array of models.