I think the best way to explain indexes is with the phone book example. The phone book is organized by Last Name, First Name. You can easily look someone up by their last name or their last name AND first name, BUT you can't easily look someone up just through their first name. To easily search through by first name only you would need a separate index.
So coming back to your example, they have an index on customer_id and product_id so you can easily find a customer_id or a customer_id and a product_id. But the first index alone doesn't allow you to search easily by product_id. So the second index allows you to easily find a product_id or a product_id and a customer_id.
If you have a has_many :through relationship
, your join table should have a unique index on both properties involved in the join as a compound key which is what you have presented.
This question and answer is a pretty good resource for some more information on when to use indexes in rails: When to add what indexes in a table in Rails