jeudi 9 avril 2020

Why many to many relationship in Rails connects also opposite way?

I have two models purchase_order and sale_order. These models are in a relation many to many with each other using has_and_belongs_to_many

The problem is that when I try to connect a purchase to a sale like:

SaleOrder.first.purchase_orders << PurchaseOrder.first

and then run in console

SaleOrder.first.purchase_orders
#it brings me the purchase order I connected. This is what I expected
PurchaseOrder.first.sale_orders
#it brings me the sale order I used to attach the purchase order. This is not what I expected.

In fact I don't want this to happen, because purchase_order.first will have is own sale_orders and I don't want the parent record. I expect to get an empty array in this case.

Aucun commentaire:

Enregistrer un commentaire