I have a Client model and a Product model where a Client has many Products and a Product belongs to a CLient.
I need to find a query that only returns Clients if they have a record in the Product table
clients table
id | name
--------------
1 | Company A
2 | Company B
3 | Company C
products table
id | name | client_id
---------------------------
1 | Product A | 1
2 | Product B | 1
3 | Product C | 3
4 | Product D | 3
5 | Product E | 1
I only need Clients 1 3
For example something like
@clients = Client.where("client exists in products") #something to this effect
@clients = Client.joins(:products)
works ? I think it'll do an INNER JOIN, which is (i think) what you want – TristanClient.joins(:products)
is returning a collection of clients right ? the problem is that you have duplicate entry that's it ? – TristanProduct.joins(:client).map(&:client).uniq
– Tristan