I have a has_and_belongs_to_many
association between product and supplier.
join table is products_suppliers
.
I am trying to create dependent selects in a form so that when I select a supplier, in the product dropdown only corresponding products appears.
I'm having a problem with the where conditions.
If I was in a situation where product belongs_to supplier I would do:
@products = Product.where("supplier_id = ?", params[:supplier_id])
How do I achieve the same in a has_and_belongs_to_many association?
Use this code:
supplier = Supplier.find_by(supplier_id: params[:supplier_id])
@products =supplier.products if supplier.present?